javascript - Rendering views for each model in a backbone collection -


i'm working on toy backbone.js application, library application perform crud operations on library. here book model , library collection (of books)

var book = backbone.model.extend({     url: function() {         return '/api/books' + this.get('id');     } });  var library = backbone.collection.extend({     model : book,     url : '/api/books' }); 

this seems pretty straightforward. next, want able show book, have bookview...

var bookview = backbone.view.extend({     tagname: 'li',     render: function() {         this.$el.html(this.model.get('author'));         $('#list-of-books').append(this.$el);     } }); 

all render method append li end of unordered list id of list-of-books in html.

next, unsurprisingly, if add following code, list 1 item (the name of author of book id=4)

var a_book = new book(); a_book.url = '/api/books/4'; a_book.fetch({     success: function() {         var bookview = new bookview({ model: a_book });         bookview.render();     } }); 

here's don't understand. add following code , nothing happens:

var some_books = new library(); some_books.fetch(); some_books.foreach(function(book) {     alert('why function not being run');     var view = new bookview({ model: book });     view.render(); }); 

for reason, can't loop code run, don't see alert pop out screen. let me know if understand what's wrong code , how can make list render properly. thanks

you calling .render inside fetch. hence execution stops on there itself. loop wont run after fetch has returned already.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -