javascript - Backbone Collection View each -


i'm new backbone.js , getting trouble collection view. here's i'm trying do:

var customerscollection = new _app.collections.customers(); var customersview = new _app.views.customers({collection: customerscollection}); customersview.render(); 

and here's view - can't understand why cannot iterate on collection:

_app.views.customers = backbone.view.extend({     render: function() {         console.log('here collection');         console.log(this.collection);         console.log('now lets iterate on it...');         _.each(this.collection, function(item) {             console.log(item);         }, this);         console.log('...done');         return this;     } }); 

what see in chrome console:

here collection child {length: 0, models: array[0], _byid: object, constructor: function, url: "/admin/customers/latest.json"…}     _byid: object     length: 5     models: array[5]     __proto__: surrogate lets iterate on it... ...done  

so can't figure out why can see collection can't each on it. thanks

// solved

i have found why going happen. missed .fetch() asynchronous, when render() called, data still not present in collection. code works me now, can go on templates, etc

_app.views.customers = backbone.view.extend({     initialize: function() {         this.collection = new _app.collections.customers();         this.collection.on('sync', this.render, this);         this.collection.fetch();     },     render: function() {         this.collection.each(function(item) {             console.log(item);         });         return this;     } });  new _app.views.customers(); 

regards, nikolay

you're not using _.each appropriately.

should be:

 _.each(this.collection.models, function(item) {    console.log(item);  },this); 

or better yet:

 this.collection.each(function(item) {    console.log(item);  }); 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -