可能重复:
Backbone.js - add parameter sending to fetch not trigger the collection
在我的功能中,尽管我添加了重置以触发我的渲染功能,但没有发生任何事情...任何线索我错误的我做了什么..?
代码:
$(document).ready(function(){
var school = {};
school.model = Backbone.Model.extend({
defaults:{
name:'name yet to decide'
}
});
school.collect = Backbone.Collection.extend({
model:school.model,
url:'js/school.json',
parse:function(response){
return response;
}
});
school.view = Backbone.View.extend({
tagName:'div',
className:'division',
template:$('#newTemp').html(),
render:function(){
var temp = _.template(this.template);
this.$el.html(temp(this.model.toJSON()));
return this;
}
});
school.views = Backbone.View.extend({
el:$('#content'),
initialize:function(){
_.bindAll(this);
this.collection = new school.collect;
this.collection.bind("reset", this.render);
this.collection.fetch({
update:true
})
},
render:function(){
var that = this;
_.each(this.collection.models, function(item){
that.addItems(newItem);
})
},
resets:function(){
console.log(this.collection.models)
console.log('calling from resets')
},
addItems:function(item){
var newItem = new school.collect({model:item});
this.$el.append(newItem.render().el);
},
changed:function(){
console.log('calling from changed')
}
});
var newSchool = new school.views;
})
答案 0 :(得分:2)
与Backbone.js 1.0.0一样你听reset
但是传递{reset:true}
选项。
当模型数据从服务器返回时,集合将是 (有效地)重置,除非你传递{update:true},在这种情况下它 将使用更新(智能)合并获取的模型