-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
there is a way to add several models with "same" id to a collection #4205
Comments
I was looking at it. Logically, you shouldn't be able to add more than one item with the same id, since the ID is unique. When you are sending the id attribute, the Backbone verifies if the id already exists or not before trying to add it. If it exists, nothing is done. The problem is that if you don't send this attribute, the Backbone does not do this verification and then, create the models with the default values, creating the bug. To fix it, need to do the verification when creating the model and not when it is doing the |
i suppose, that attribute defined in i can suggest this small solution Line 406 in 27f7d41
i think, that this part can be replaced with this few lines
|
I've been investigating this issue, and I realized that the problem is not in Backbone, but in the hypothetical user violating the preconditions of the framework. If you pass a raw hash of attributes to a collection, the collection must be able to establish, purely based on that hash, whether it represents a model that is already in the collection or not. It does that by passing the hash through I could I could do an While the hypothetical singleton pattern thing is weird, that use case is more valid than one in which the I could do a hardcore refactor of Instead of all these things, I will simply submit a PR to document this gotcha in |
I use a modified Backbone, which allows disabling the unique ID feature and enables adding multiple models with the same ID to the same collection: idAattribute: null A breaking change in Backbone 1.4 made this code change mode complicated. It introduced mandatory polymorphism - using modelId: function (attrs, idAttribute) {
if (idAttribute === undefined || !this.polymorphic) {
idAttribute = this.model.prototype.idAttribute;
}
return idAttribute !== null ? attrs[idAttribute || "id"] : null;
}, |
@prantlf That is interesting. But why mention it here? What was the breaking change in Backbone 1.4? |
Because this topic is: "there is a way to add several models with "same" id to a collection". The breaking change was the now mandatory polymorphism as I wrote above. The code allowing not unique IDs like the code above, but for Backbone older than 1.4: modelId: function(attrs) {
var idAttribute = this.model.prototype.idAttribute;
return idAttribute !== null ? attrs[idAttribute || 'id'] : null;
}, |
@prantlf I do think that your reply is about a different issue. The current issue is about Are you just pointing out that a breaking change was made in 1.4, or do you also mean to request another change? In the first case, I will acknowledge that it was a breaking change. The author intended it to be a backwards-compatible enhancement, but changed existing tests, which is generally a clear indication of breaking changes. See #3966. In the second case, please check out #3966 and the other issues linked from there. You might find the comments in there illuminating, and you might find a more appropriate place to respond as well. If you still want to suggest changes to Backbone after that, I encourage you to open a new ticket or pull request. |
It appears that I misunderstood this issue. I thought and it still kind of looks to me that it's about using the I'm not sure if disabling the ID with |
Please feel welcome to open a new ticket for your usecase. Maybe we can come up with a better solution! |
default normal behavior:
but, if we did that with model having id in defaults then all models will be added to a collection
working fiddle: https://jsfiddle.net/gkpLoaqx/
maybe id field from defaults should not be applied on model initialize?
The text was updated successfully, but these errors were encountered: