在我的主干功能中,如何在添加新元素之前删除元素?,我的功能一切正常。问题是当我单击“更新”时,它正在更新元素,并且仍然可以使用旧版本。如何删除旧元素,并从更新调用中追加新元素..?
清除现有视图元素并附加新集合的正确方法是什么?
我的功能:
$(document).ready(function(){
var school = {};
school.model = Backbone.Model.extend({
defaults:{
name:'no name',
age:'no age'
}
});
school.collect = Backbone.Collection.extend({
model:school.model,
url:'js/school.json'
});
school.view = Backbone.View.extend({
tagName:'div',
className:'member',
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'),
events:{
'click #newData' : 'newArrival',
},
initialize:function(){
_.bindAll(this);
this.collection = new school.collect;
this.collection.bind('reset', this.render);
this.collection.fetch();
},
newArrival:function(){
school.views.remove(); // i tried this, throw the error
this.initialize();
},
render:function(){
var that = this;
_.each(this.collection.models, function(item){
that.renderItem(item);
})
},
renderItem:function(item){
//how can i remove the older elements and append new alone?
var newItem = new school.view({model:item});
this.$el.append(newItem.render().el); // keep adding but previous element still not removed,
}
});
var newSchool = new school.views;
})