javascript - How to fire render when a views collection is reset -
i'm starting play around backbone. i've got view associated collection, , want render view when collection syncs server.
i've managed collection sync -
var mycollection = backbone.collection.extend({ model: backbone.model, url: '/api/cart/lineitem' }); var mycollection = new mycollection(); mycollection.fetch({ success: function() { console.log('fetched ' + mycollection.length + ' objects'); } });
the console shows fetch function works.
however i'm getting strange behaviour in view. can't seem render function run.
var myview = backbone.view.extend({ el: $('#mini_cart'), carttpl: _.template($('#minicarttemplate').html()), initialize: function() { this.listento(this.collection, 'reset', this.render); this.listento(this.collection, 'reset', console.log('collection reset')); }, render: function(){ console.log('rendering myview'); } }); var myview = new myview({ collection: new mycollection() });
the console shows event fires never enters render
method (ie 'collection reset' message never 'rendering myview' message). don't understand what's going on (i don't see how reset
event being fired on collection @ all).
this.listento(this.collection, 'reset', console.log('collection reset'));
the third paramter of listento has function, not method call (which doesn't return function). try remove line or wrap console.log call function this:
this.listento(this.collection, 'reset', function(){console.log('collection reset');});
Comments
Post a Comment