Javascript Backbone.js将对象从这个转换为那个,并使用每个抛出错误调用
在主干函数中,我为自己创建了一个名称空间,名为Javascript Backbone.js将对象从这个转换为那个,并使用每个抛出错误调用,javascript,backbone.js,backbone-views,Javascript,Backbone.js,Backbone Views,在主干函数中,我为自己创建了一个名称空间,名为var taskListPhraseI={}在使用此名称空间时,所有我的功能都已分配 当我从collection类中获取集合时,使用传递给renderBoard的each函数,在each将不知道this关键字的这一点上,我使用声明为that并分配给this的变量,使用this调用该函数,但是渲染函数将错误作为 TypeError: that.renderBoard is not a function [Break On This Error]
var taskListPhraseI={}代码>在使用此名称空间时,所有我的功能都已分配
当我从collection类中获取集合时,使用传递给renderBoard
的each函数,在each将不知道this
关键字的这一点上,我使用声明为that并分配给this的变量,使用this
调用该函数,但是渲染函数
将错误作为
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
而不包含其上下文,该上下文可以通过下划线库中的来修复。作为信息,我的render函数已经可以正常工作,从render函数我无法调用renderBoard。您尝试过我的解决方案吗render
函数本身是正确的,但是fetch
函数应该将正确的上下文this
传递给success
回调函数。是的,这是有效的。你能提供一些关于这个问题的信息吗?当我把东西传递给渲染器时,这是渲染器传递给渲染器板的责任,那么这里的工作是什么呢?我在回答中添加了bind
链接,希望能有所帮助。是的,我得到了。谢谢你的快速重播。作为一名新生,我刚刚开始我的项目。