0

我有以下一段代码

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));
 }

我可以将“passedModel”添加到 NewCollection,但不能删除它们。我做错了什么,我应该如何更正我的代码?

4

1 回答 1

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});
}

谢谢大家!

于 2012-12-13T14:10:48.010 回答