当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - listenTo not firing event

问题描述:

I'm trying to execute my view's render method but for some reason it is telling me that Uncaught TypeError: Cannot call method 'listenTo' of undefined, not quite sure why.

var App = Backbone.View.extend({

current_election_index: 0,

el: 'body',

initialize: function() {

elections = new Elections();

_.bindAll(this, 'render');

this.listenTo(this, 'change', this.render);

elections.fetch();

/* elections.fetch({

success: function(test) {

console.warn(this.App.render());

this.render();

}*/

// });

},

render: function () {

console.log('this is the render method');

var view = new ElectionView({model: elections.at(0)})

}

})

网友答案:

What you want to do is listen to elections. So rather than listening to this.model or this,

this.listenTo(elections, 'reset', this.render);

The reset is triggered on a collection "when the collection's entire contents have been replaced". The change event is triggered on a model "when a model's attributes have changed". See the Backbone Catalogue of Events for more information.

If you want to update the view when each model belonging to the elections collection changes, be sure to do that in each ElectionView subview, not the App view.

分享给朋友:
您可能感兴趣的文章:
随机阅读: