goodeggs / angular-cached-resource

An AngularJS module to interact with RESTful resources, even when browser is offline
MIT License
216 stars 29 forks source link

Cached array responses should update in place when http response arrives #9

Open hazeledmands opened 10 years ago

hazeledmands commented 10 years ago

Imagine you have a cached resource with some form of identifying characteristic like an id:

var CachedResource = $cachedResource('example', '/example/:id', { id: '@id' })

Then you query CachedResource and get back something from cache right away:

var list = CachedResource.query(); // [ { id: 1, name: 'One' }, { id: 2, name: 'Two' } ]
var cachedItem = list[0];  // { id: 1, name: 'One' }

But lets say the response comes back as something different:

list.$httpPromise.then(function() {
  list; // [ { id: 1, name: 'Uno' }, { id: 2, name: 'Dos' } ]
});

It'd be great if BOTH of these expressions reflected the response from the server:

list.$httpPromise.then(function() {
  list[0].name; // 'Uno'
  cachedItem.name; // 'One'
});
lgomezma commented 10 years ago

+1, I have the same issue.

makebbekus commented 10 years ago

Unless the cachedItem has been modified before the $httpPromise resolves which would mean regardless of what comes back from the server, a local change has been made.

Although maybe it'd be ideal to have the resource keep track of local changes separately from the most current data from the server...

hazeledmands commented 10 years ago

@makebbekus You're absolutely right; what do you think is the correct behavior in that circumstance? Just ignore the server version of the attribute if there's a local change to it?

makebbekus commented 10 years ago

@demands yeah, for now that's what this commit does https://github.com/goodeggs/angular-cached-resource/blob/6e6738886815f8b77e7637ed74da9b452b1fe1f7/src/modify_object_in_place.coffee#L9

But it does seem like there's a case where you'd be making a local change on an object and then want to "undo", and you might as well have the latest information from the server. To achieve this I think we'd need a more sophisticated way of storing resource edits separate from the last known version on the server.