-
Notifications
You must be signed in to change notification settings - Fork 27
Description
There is a whole slew of problems once you add an element with id:0.
For example, if you keep performing collection.set(collection.serialize())
(which should be more or less idempotent) you get more and more elements appended to the end, all of which have the same .cid
.
Moreover you can not clean up this collection as collection.set([])
is unable to remove them.
I think the problem is in this line:
if (order && ((model.isNew && model.isNew() || !model[this.mainIndex]) || !modelMap[model.cid || model[this.mainIndex]])) order.push(model);
In particular the condition !model[this.mainIndex]
treats id=0 as a missing id, and subsequently schedulles the model for ordering phase.
In that phase it gets pushed at the end:
var orderedModels = order || toAdd;
for (i = 0, length = orderedModels.length; i < length; i++) {
this.models.push(orderedModels[i]);
}
It's also important that the model pushed to the order
array is actually the already existing one found by get(0)
, which means that we push exactly the same instance at the end of this.models
array, so now we have two entries with the same .cid
.
This in turn means that this elements never get purged, even if one calls set([])
, because of two other problems (I think there is a separate bug for that in issue tracker) - one is that there is
if (query == null) return;
in get(query)
function, and for some reason the toRemove
array contains undefined
as values.
The second problem is that once any element with given cid is removed it is also removed from _indexes.cid. So if there are multiple such elements, once the first is removed, then subsequent are irremovable, due to:
model = models[i] = this.get(models[i]);
if (!model) continue;
in remove()
.
I think the two problems are related in that, once there is an element missing from the index, it is quite possible that undefined
will occur in the toRemove
array.
In general, I'd suggest scanning the whole code against expressions like "!x" and carefully replacing them with more semantically valid x === undefined
or blah in x
or whatever the programmer actually had in mind.