在我的主干函数中,我为我创建了一个 namespace ,如 var taskListPhraseI = {};
在使用这个 namespace 时,我的所有函数都被分配了。
当我从集合类中获取集合时,使用我传递给renderBoard
的每个函数,在每个函数中,每个函数都不会知道此时的this
关键字,我使用了一个声明为该变量并分配给 this 的变量,使用 that
我正在调用该函数,但 render 函数
抛出错误为
TypeError: that.renderBoard is not a function
[Break On This Error]
that.renderBoard(item)
但我确实有 renderBoard 函数。有给我解决这个问题的线索吗?
我的部分代码:
taskListPhraseI.allView = Backbone.View.extend({
el:$('.boardHolder'),
initialize:function(){
this.collection = new taskListPhraseI.collection();
this.collection.fetch({success:this.render});
},
render:function(data){
var that = this;
_.each(data.models, function(item){
that.renderBoard(item) // throw the error..
},this);
},
renderBoard:function(item){
console.log('item'); // i am not getting the call here...
}
});
最佳答案
尝试将 this.collection.fetch({success:this.render});
替换为 this.collection.fetch({success:_.bind(this.render, this )});
.
您传递的函数 this.render
没有其上下文,这可以通过 bind
修复在下划线库中。
关于javascript - Backbone.js 将对象从 this 转换为 that 并使用每个抛出错误进行调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14474317/