下面是我的 View 初始化函数,它在初始化时应该接收模块名称,以便从集合中获取正确的数据。
问题是:
- Listento 在获取集合后没有重定向到 render() 方法,它还在控制台上给我一个错误
TypeError: e is undefined
我在下面的代码中犯了什么错误?
initialize: function() {
var that = this;
if(this.options.module === 'questions'){
require([
'app/collections/questions'
], function(QuestionsCollection){
var moduleCollection = new QuestionsCollection();
that.collection = moduleCollection;
moduleCollection.fetch({
reset: true,
success: function(){},
error: function(){}
});
});
}
this.listenTo(this.collection, 'reset', this.render);
this.listenTo(Backbone, 'close:Home', this.close);
},
最佳答案
我认为这是一个范围问题,其中 require 模块处于闭包中。尝试以下操作:
initialize: function() {
var that = this;
if(this.options.module === 'questions'){
require([
'app/collections/questions'
], function(QuestionsCollection){
var moduleCollection = new QuestionsCollection();
moduleCollection.fetch({
reset: true,
success: function(){},
error: function(){}
});
that.listenTo(moduleCollection, 'reset', that.render);
});
}
this.listenTo(Backbone, 'close:Home', this.close);
},
关于javascript - 获取集合后不会调用集合重置上的 listenTo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21662733/