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
Collection#fetch updates existing model instances (vs replacing) #955
Comments
I could see an option being created for this, but the default behavior should still remain |
How would you want to handle "add" events to the collection if you're only updating a model? Would you want your change event to fire instead? I can see situations where both would be valid behaviors (throw-away views vs carefully managed views etc.) |
Yes, I've implemented something like this, adding/removing as necessary instead of replacing. I call it a "soft" reset. It doesn't support updating the models but it certainly could. Very handy. |
We've used custom extension to do exactly that in Wunderkit as well - 👍 from us on this one - and yes - it should be as an option rather then default behavior edit: hm I'm wondering if we could implement kind of "sync" behavior here - for example a fetch returns same collection - but some models are gone, some are changed and some are added. Now it should not only check vs models if they already exist but it should also check if any models weren't removed so they should get removed from the collection as well. Something to think about :) |
There used to be a pull request, I have not checked whether the snippet works with Backbone 0.9.1 but it should get you on track dalyons@0af0e23 |
update existing models collection.fetch({update: true}); or collection.update(models); update existing models but prune missing collection.fetch({update: true, prune: true}); or collection.update(models, {prune: true});
@dalyons Why is there no PR for this awesome feature? Because it works like a charm! |
I'd be glad to entertain a pull request for "smart" resets, but -- they're already easy to accomplish with the tools backbone gives you: Furthermore, you'd have to deal with a number of API choices, most of which have no "right" answer:
|
If I create a new model, and then call fetch, it will update that model's attributes.
If I create a collection of models, and then call fetch, it won't update the existing models – it will replace them with new instances.
This is painful because I cannot cache references to the collection's model instances, as the references become invalid once a fetch occurs. Instead I'd have to cache references to the collection, and do Collection#get every time I want to access the model. Maybe that's acceptable, but it seems reasonable that a collection should be able to perform a fetch/update without invalidating existing model instances.
The text was updated successfully, but these errors were encountered: