[英]Backbone.js converting object from this to that and calling using each throw errror
在我的主干函數中,我為我命名空間為var taskListPhraseI = {};
在使用此名稱空間時,我的所有功能均被分配。
而我取出由集合類集合,使用各個功能我傳遞到renderBoard
,在每次都會不知道關於this
在這一點上鍵字,我用一個變量聲明為並分配到這一點,使用that
我是調用函數,但render function
將錯誤拋出為
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});
with this.collection.fetch({success:_.bind(this.render, this)});
。
您傳遞的函數this.render
沒有上下文,可以通過下划線庫中的bind
進行固定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.