Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 模型更新后视图将丢失其模型_Backbone.js_Backbone Views - Fatal编程技术网

Backbone.js 模型更新后视图将丢失其模型

Backbone.js 模型更新后视图将丢失其模型,backbone.js,backbone-views,Backbone.js,Backbone Views,所以我在用这个骨干剧本战斗。 基本上,它应该做的是:当模型中的某些内容发生更改时,它需要再次渲染视图 一切进展顺利,更改事件被触发,视图响应。 但是当我回到渲染中时,它丢失了模型,也丢失了视图中需要渲染日期的元素($el) 我创建了一个片段,您可以在这里找到它: var-APP={}; APP.items=[{ “id”:“id_2”, “别名”:“第2项” }]; APP.newItem2={ “id”:“id_2”, “别名”:“第2项”, “ttile”:“这是你的头衔”, “描述”:“

所以我在用这个骨干剧本战斗。 基本上,它应该做的是:当模型中的某些内容发生更改时,它需要再次渲染视图

一切进展顺利,更改事件被触发,视图响应。 但是当我回到渲染中时,它丢失了模型,也丢失了视图中需要渲染日期的元素($el)

我创建了一个片段,您可以在这里找到它:

var-APP={};
APP.items=[{
“id”:“id_2”,
“别名”:“第2项”
}];
APP.newItem2={
“id”:“id_2”,
“别名”:“第2项”,
“ttile”:“这是你的头衔”,
“描述”:“我还需要多说吗?”
}  
APP.ItemModel=Backbone.Model.extend({
初始化:函数(attr){
控制台日志(attr);
},  
updateStore:函数(prod){
这个。设置(prod);
}  
});
APP.ProductsCollection=Backbone.Collection.extend({
型号:APP.ItemModel,
初始化:函数(mdls){
console.log(mdls);
}
});
APP.ItemView=Backbone.View.extend({
初始化:函数(){
this.model.on(“change”,this.render);
这个。render();
},  
渲染:函数(){
console.log('this:',this);//这始终是已知的
console.log('render model:',this.model);//第二次未定义模型时
console.log('el:',this.$el);//第二次未定义$el
var htmlSource=$(“#itemContent”).html(),
template=handlebar.compile(htmlSource),
compiled=模板(this.model);
这是.$el.html(已编译);
},
活动:{
单击。单击BTN:“加载数据”
},
loadData:函数(e){
this.model.updateStore(APP.newItem2);
}
});
APP.MainView=Backbone.View.extend({
el:#列表容器“,
初始化:函数(){
APP.productsCollection=新APP.productsCollection(APP.items);
这个。render();
},
渲染:函数(){
var=这个,
htmlSource=$(“#基本内容”).html(),
template=handlebar.compile(htmlSource),
编译=模板(APP.productsCollection);
这是.$el.html(已编译);
APP.productsCollection.models.forEach(函数(模型){
新建APP.ItemView({
el:“#”+model.attributes.id,
型号:型号
});
});           
}        
});
APP.mainView=新建APP.mainView()
.maplayer\u wrap{
宽度:500px;
高度:100px;
背景色:白色;
填充:10px;
边缘顶部:10px;
边框:1px实心#CCC;
}

{{{#每个模型}
加载。。。
{{/每个}}
新数据。。。

{{{attributes.title}

{{attributes.description}

点击
找到了

在我设置侦听器的视图中,我现在也添加了“this”,现在它可以工作了

问题:

解决方案:
找到了

在我设置侦听器的视图中,我现在也添加了“this”,现在它可以工作了

问题:

解决方案:

你应该。你应该。
this.model.on("change", this.render);
this.model.on("change", this.render, this);