Reputation: 385
I'm having this problem which is driving me nuts. I have a collection of entity, named entities which receives A and B objects. The following code, as simple as it may look, does not work as (I) intended. Somehow the last object being added to the entities collection is not being added. In fact if I print the lengths before and after I get something like 1, 2, 3, 4, 5, 5 instead of the expected 1, 2, 3, 4, 5, 6. A and B are "subclasses" of Entity which is a model, being that both A and B have their corresponding views. In the end I'm expecting 6 visible objects but I only get 5. Has anyone got any idea of what the problem might be? Thanks in advance.
var that = this;
_.each( this.as.models, function( a, i ){
that.entities.add( a );
});
_.each( this.bs.models, function( b, j ){
that.entities.add( b );
});
Upvotes: 2
Views: 200
Reputation: 309
When you add objects or Backbone models to a Backbone collection a check is made to find whether that object is not already present in the collection. In case an existing object is inserted nothing is changed, no events are fired and so on. An object is considered the same either by equality or by matching id
attributes.
There is a way to override this by passing the {merge: true}
option to the add
method.
Upvotes: 2