Closed jSanchoDev closed 1 year ago
This does seem like it might cause collisions if your idAttribute format clashes with the default return values of _.uniqueId
(which is used to generate the value of this.cid
).
You could set the cidPrefix to something else than the default (c
) to make sure it doesn't clash with your idAttribute
to see if that fixes your problem.
Thanks a lot, @jaapz, this indeed fixes this and it's a very simple solution too, which is great. Perhaps it's a good idea to add some sort of warning in docs related to idAttribute
section? It's a kind of bug that can be hard to reproduce and find the cause of. Thanks again!
Hi there!
We're having a strange issue when using a collection of models with custom
idAttribute
. Basically, ifidAttribute
attribute has a value that "looks like"cid
(c12, c78, etc), the model is not added to collection or merged with some previously added model.Please, check the following simplified example:
Or check this fiddle: https://jsfiddle.net/0goy4u9w/8/
I believe this happens because Backbone stores models both by
cid
andidAttribute
, when available, here: https://github.com/jashkenas/backbone/blob/75e6d0ce6394bd2b809823c7f7dc014ddb6ae287/backbone.js#L1178-L1183 Then, duringset()
it finds the previously stored (bycid
) model and assumes it's found a duplicate: https://github.com/jashkenas/backbone/blob/75e6d0ce6394bd2b809823c7f7dc014ddb6ae287/backbone.js#L870-L877Now, I understand that this looks like someone's tried really hard to find a defect 😬and created ids that look like cids, but we actually use custom id attributes that are unique, but don't follow same pattern (they come from different sources), and sometimes they actually look like cid. 😱
Is there a reason why
cid
is still being used as key for_byId
even when models haveidAttribute
declared?