Skip to content

Find which items from collection have changed with lastModified #159

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

Closed
xvaldetaro opened this issue Sep 12, 2014 · 5 comments
Closed

Find which items from collection have changed with lastModified #159

xvaldetaro opened this issue Sep 12, 2014 · 5 comments
Assignees
Milestone

Comments

@xvaldetaro
Copy link

Hi,

Is it possible to get which items have been modified from one lastModified call to another?

E.g.

var hash1 = DS.lastModified('document'); 
// Change some documents with update, save or inject
var hash2 = DS.lastModified('document');
DS.getModifiedItems(hash1, hash2);

Thanks a lot for the project, it is making my life much easier. I have been looking around for something like this since I saw the breeze.js video.

@jmdobry
Copy link
Member

jmdobry commented Sep 12, 2014

It's not possible right now. Angular-data doesn't keep a trail of hash changes, only the most recent.

I supposed I could implement a change history of configurable length. Each entry could include the timestamp of the change, the resourceName and id of the changed item, and the changes themselves. You could gather from that which items have changed. What do you thinK/

@xvaldetaro
Copy link
Author

Looks like a very useful feature IMO. Besides from being able to pinpoint changes in a big collection, you give an undo feature for free.

The project I am working on is now leaving all the data for angular-data to manage, except for one resource that has a big collection and changes to individual documents must be observed by many parts of the application. This resource ATM needs a manager that dispatch events for updates in the collection, so I still keep this resource out of angular-data. I even thought about watching modifications for each individual document via lastModified but would be too much pain.

@jmdobry
Copy link
Member

jmdobry commented Sep 12, 2014

Here's a thought:

DS.defineResource('big-resource');
$scope.$watch(function () {
  DS.store['big-resource'].lastModified;
}, function (hashes, prevHashes) {
  var changedIds = [ ];
  angular.forEach(hashes, function (timestamp, id) {
    if (timestamp !== prevHashes[id]) {
      changedIds.push(id);
    }
  });

  // array of ids of items that changed
  console.log(changedIds);
}, true);

Edit: nevermind, this won't be sufficient

@xvaldetaro
Copy link
Author

Correct me if I am wrong, but I am guessing that it is possible that a resource gets changed (and timestamped) multiple times between one $digest and another. Consequently, $watchers will be notified once via dirty checking and will think that only the last timestamp updates are the ones counting. So if the $watches cherry pick the updated documents they will miss previous updates.

If that is the case, isn't there a way to guarantee that all changes between two $digest are timestamped together?

@jmdobry
Copy link
Member

jmdobry commented Sep 12, 2014

If the browser doesn't support Object.observe then I don't think objects get timestamped multiple times between digest cycles. The digest cycles of angular-data and your app are tied together if your browser doesn't supportObject.observe`.

The change stream seems like a better option.

@jmdobry jmdobry added this to the 1.0.0-rc.2 milestone Sep 15, 2014
@jmdobry jmdobry self-assigned this Sep 15, 2014
jmdobry added a commit to js-data/js-data that referenced this issue Sep 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants