我有以下代码
initialize : function(option){
//some code
this.PassedCollection = this.options.serverResponse.passedCollection;
this.NewCollection = new Collection();
this.NewSectionCollection.bind("add", this.add, this);
this.NewSectionCollection.bind("remove", this.remove, this);
//some code
}
//some other code
addRemove: function(){
this.PassedCollection.forEach(_.bind(function(passedModel){
if(passedModel.reference==event.target.id){
if($('input[name='+passedModel.reference+']').attr( 'checked')){
console.log("checked");
this.NewCollection.add(passedModel);
}
else{
console.log("unchecked");
this.NewCollection.remove(passedModel, {silent : true});
console.log(JSON.stringify(this.NewCollection));
}
}, this));
}
我可以将传递的模型添加到NewCollection中,但不能删除它们。我做错了什么以及如何更正我的代码?
答案 0 :(得分:0)
添加到集合中的passedModel的cid与为了从集合中删除模型而传递的passedModel的cid不同。 我做了以下工作,它对我有用: (只是else部分中的代码):
else{
console.log("unchecked");
var model = _.find(this.NewCollection.models, function(model) {
return model.get("reference") == passedModel.reference;
});
this.NewCollection.remove(model, {silent : true});
}
谢谢大家!