0

I have the following piece of code

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

I can add the 'passedModel's to the NewCollection, but cannot remove them. What am I doing wrong and how should I correct my code?

JeanFrancois
  • 134
  • 3
  • 7
  • It's tough to tell what's going on without more context. Could you maybe post a short Fiddle that reproduces the problem? http://jsfiddle.net/Rm8Xb/ – McGarnagle Dec 12 '12 at 22:33

1 Answers1

0

The cid of the passedModel added to the collection was not the same as the cid of the passedModel that was passed in order to remove the model from the collection. I did the following and it works for me: (Just the code in the else part):

else{
    console.log("unchecked");
    var model = _.find(this.NewCollection.models, function(model) {
        return model.get("reference") == passedModel.reference;
    });
    this.NewCollection.remove(model, {silent : true});
}

Thanks everyone!

JeanFrancois
  • 134
  • 3
  • 7