Skip to content
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

Backbone polymorphism does not support two models with same ID #2043

Closed
gabetax opened this issue Dec 28, 2012 · 7 comments
Closed

Backbone polymorphism does not support two models with same ID #2043

gabetax opened this issue Dec 28, 2012 · 7 comments
Labels

Comments

@gabetax
Copy link

gabetax commented Dec 28, 2012

Following the polymorphic collection instructions in #793, there seems to be an issue if you have two different models types (saving to two different URLs, and ultimately two different database tables) with the same id

collection = new App.Collections.PaymentsCollection(
  {"id":6,"amount":"2000.0","class_name":"CreditCard"},
  {"id":6,"amount":"1000.0","class_name":"ECheck"}
)
collection.models.length
>>> 1
collection.models
>>> [CreditCard]

I can understand why a collection would want to remove duplicate models of identical objects, but I think it's a reasonable use case to have a polymorphic collection that may have models of different classes but with the same id.

The main sticking point seems to be in collection.get, which collection.add uses in its duplication check.

Do you have any opinions on the best way to handle this polymorphism use case? Up until this one issue, the solution from #793 has felt easy and elegant to me.

@caseywebdev
Copy link
Collaborator

It looks like you need a composite key. I created a plugin at https://github.com/caseywebdev/backbone-composite-keys but it hasn't been updated for 0.9.9.

@gabetax
Copy link
Author

gabetax commented Dec 28, 2012

A specific temporary solution I can think of is to override the PaymentsCollection.get(model) method to run a _.find on the collection that accounts for class type and id.

For a long term solution perspective, I'm concerned that this may require too much knowledge about the internals of how collection duplication filtering works, especially given that this functionality seems to have changed recently in both 0.9.9, and then again in the latest master branch. I'm also uncertain of the performance impact in scenarios where the collection has several models.

@gabetax
Copy link
Author

gabetax commented Dec 28, 2012

@caseywebdev your plugin looks like a workable solution to this problem too. I'll give it a shot, thanks!

@caseywebdev
Copy link
Collaborator

Again, if you're on master the plug-in may have unexpected side effects. Feel free to modify it to account for the new changes.

@gabetax
Copy link
Author

gabetax commented Jan 7, 2013

There's more discussion about this type of problem in #1558 which spawned caseywebdev's composite keys plugin

@caseywebdev
Copy link
Collaborator

@gabetax I actually just updated my plugin to work correctly with master

@gabetax
Copy link
Author

gabetax commented Jan 7, 2013

@caseywebdev I updated earlier today when I saw it, and it seems to be working well for me so far. 🎉

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants