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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -