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 に答える