-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Should Backbone.Collection throw an error when client code attempts to add the same model twice? #4250
Comments
Thanks @paulfalgout. Seems like the old throwing behavior was not only on What do you think? Should a collection throw in the first case but not the second? Or should we just stick to the current behavior? |
My initial reaction is to suspect that handling the first case and not the second is too costly a check to add to I might be running down a rabbit trail, but I started to wonder what the point of the But then where the merge option was added #1220 Honestly I don't understand the use case of using |
Taking the effort to throw an error would imply it's bad practice, rather than simply an unusual use-case. Which makes me wonder, what specifically is the problem and why? Are multiple identical id's a problem? Are multiple copies of the same data a problem? Are multiple instances of the same model a problem? I haven't used Backbone in a while so I might be a little rusty in terms of thinking of undesirable side-effects, but I can't think of a reason why in principle any of those should not be allowed. I can think of a scenario that doesn't require multiple copies of the same record but still would see multiple models with the same ID. For example, one could push multiple new records onto a collection, each of which have not yet been assigned an ID by the server. One could then push the entire collection to the server to be committed and receive an updated version of the collection data in return where each record now has been assigned it's own unique ID. While most likely not a common scenario at all, I would also hesitate to say the ability to have the same record present in a collection more than once is never going to be useful. Finally, if backbone does allow multiple copies, why not allow multiple references to the same instance as well? This way if you update once, the entire collection remains up-to-date. It would be significantly more complicated to keep multiple copies up-to-date instead. |
New models without Primarily I think the most striking reason for not having copies per id is that If for some reason the user is expecting |
@paulfalgout Right, nowadays @Rayraz Backbone's contract is that model id's need not be set, but if they're set, they must be unique (that's mentioned in the documentation in the |
The getter shouldn't be too much of a problem for major version update. It'd probably be relatively easy to implement something like However, if the index and |
While studying #4249, I found this old comment to another ticket by @jashkenas, in which he writes that Backbone once used to throw an error in this scenario: #2976 (comment).
It is ambiguous from this comment alone whether the throwing was an "enhancement", or the removal of the throwing. It would not be the first time that a feature is inadvertently removed. If
aCollection.add([{id: 1}, {id: 1}])
was supposed to throw (which intuitively makes sense) and this was removed for no good reason, then I suggest reinstating this behavior in the future Backbone 2.0. First, though, we should investigate what happened exactly and why.The text was updated successfully, but these errors were encountered: