Backbone.dualStorage icon indicating copy to clipboard operation
Backbone.dualStorage copied to clipboard

Properly account for partial updates (fetching a subset of a collection).

Open mikeric opened this issue 10 years ago • 1 comments

For perf reasons, our API accepts a since parameter and only returns items that have changed since that time (typically it's the last time that the particular client has synced), so that we can update only the models that have changed on the client.

When fetching from the API in this way, we need to pass {remove: false} to the fetch (this is so that the unchanged items are not removed from the collection).

items.fetch({
  data: {since: 1392487594},
  remove: false
})

The issue here is that all items which were not included in that fetch's response are removed from localstorage (essentially ignoring the {remove: false} option that we've set on the fetch), so even though the collection has all the items at this point, the next time you fetch from localstorage, they will be gone, and you'll only end up with the items that were returned from the last fetch.

Is there an alternative method to accomplish this sort of thing using Backbone.dualStorage? If not, would it be trivial enough to store the actual collection's models in localstorage (after a fetch) rather than just the request's response data?

mikeric avatar Feb 15 '14 20:02 mikeric

Yes, you're right that dualStorage does not currently account for remove: false. I don't know of any workaround for your situation. We should add support for this option when fetching collections.

Adding support may be as simple as changing backbone.dualstorage.coffee:272 to:

localsync('clear', model, options) unless options.add or options.remove == false

I do not have time to test this out right now. First, see if this change fixes your specific issue in your project, and report back. If possible, please create a pull request that adds a unit and/or integration test that reproduces this failure before a fix is applied, and with the fix, if it works. See the README for test and build instructions.

Thanks!

nilbus avatar Feb 16 '14 03:02 nilbus