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

adding element with id:0 results in duplicates #54

Open
qbolec opened this issue Mar 22, 2015 · 0 comments
Open

adding element with id:0 results in duplicates #54

qbolec opened this issue Mar 22, 2015 · 0 comments

Comments

@qbolec
Copy link

qbolec commented Mar 22, 2015

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.

hermann-p added a commit to campudus/tableaux-frontend that referenced this issue Nov 17, 2017
This is caused by a bug in ampersand-collection, reported in 2015 and still unfixed: AmpersandJS/ampersand-collection#54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants