Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undesired model remove event triggered #15

Open
mauron85 opened this issue Jul 9, 2015 · 1 comment
Open

Undesired model remove event triggered #15

mauron85 opened this issue Jul 9, 2015 · 1 comment

Comments

@mauron85
Copy link

mauron85 commented Jul 9, 2015

Description: When model is removed from subcollection also model remove event is triggered.

Expected: when model does not satisfy filter condition anymore, only subcollection remove event should be triggered

Actual: also model remove event is triggered

Following test will fail:

  describe 'updating model', ->
    it 'should not trigger remove on the model', ->
      filter = ->
         model.get('prop') == 'val'
      parent = new Collection
      child = new Collection
      model = new Model({id:1})
      parent.add(model)
      spy = sinon.spy()
      model.on 'remove',spy
      subset = new Subset(parent:parent,child:child,filter:filter)
      expect(child.length).to.equal 0
      model.set({prop: 'val'})
      expect(child.length).to.equal 1
      model.set({prop: null})
      expect(spy.called).to.be.false

How to reproduce:
http://jsfiddle.net/mauron85/8axcsvot/3/

@mauron85
Copy link
Author

Actually, after bit thinking I realized that it's not an issue, but my wrong assumptions.

I should not listen to model remove event, without checking which collection it was removed from.

Always do the check (probably in your view), like this:

var parent = new Collection;
var child = new Collection;
var model = new Model;
var subset = new Subset({
  parent: parent,
  child: child,
  filter: //some filter here
});

model.on('remove', function(model, collection, options) {
  if (collection === child) {
    //now you can be sure model was removed from child subcollection and from parent
  }
 });

And full test:

    it 'should not trigger remove on the model', ->
      filter = ->
        model.get('prop') == 'val'
      parent = new Collection
      child = new Collection
      model = new Model({id:1})
      parent.add(model)
      model.on 'remove', (model, collection, options)->
        expect(collection).to.equal child
      subset = new Subset(parent:parent,child:child,filter:filter)
      expect(child.length).to.equal 0
      model.set({prop: 'val'})
      expect(child.length).to.equal 1
      model.set({prop: null})

I will add remark in README.md to be aware of this, to not making same mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant