I've got a sub-collection that filters on an attribute value.
When I iterate on this sub-collection to modify attribute's value (the modification should remove the model from the collection), only half of the models are concerned by the modification.
coll = new Backbone.Collection();
for(var i=0; i<10; i++){
coll.add(new Backbone.Model({
id : 'model_'+i,
selected: true
}));
}
sub = coll.subcollection({
filter: function(model) {
return model.get('selected');
},
triggers: 'selected'
});
console.log('sub.length '+sub.length);
// sub.length 10
sub.each(function(model) {
console.log('unselect '+model.get('id'));
model.set('selected', false);
});
// unselect model_0
// unselect model_2
// unselect model_4
// unselect model_6
// unselect model_8
console.log('sub.length '+sub.length);
// sub.length 5
I reproduced the bug in http://jsbin.com/elisor/3/edit.
I'm not sure it's a bug, since iterating on a collection that modifies itself is maybe not a feature, but I would like to know the workaround in this case.
However, Backbone handles well the case of removing of model while iterating on a collection. EDIT: backbone does not support that (http://jsbin.com/elisor/2/edit)
I've got a sub-collection that filters on an attribute value.
When I iterate on this sub-collection to modify attribute's value (the modification should remove the model from the collection), only half of the models are concerned by the modification.
I reproduced the bug in http://jsbin.com/elisor/3/edit.
I'm not sure it's a bug, since iterating on a collection that modifies itself is maybe not a feature, but I would like to know the workaround in this case.
However, Backbone handles well the case of removing of model while iterating on a collection. EDIT: backbone does not support that (http://jsbin.com/elisor/2/edit)