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

lastModified changes when loading distant related objects #140

Closed
Patrik-Lundqvist opened this issue Aug 26, 2014 · 6 comments
Closed

lastModified changes when loading distant related objects #140

Patrik-Lundqvist opened this issue Aug 26, 2014 · 6 comments
Assignees
Milestone

Comments

@Patrik-Lundqvist
Copy link

DS.lastModified will change when a distant object gets loaded which makes it hard to detect changes for a specific object.

DS.defineResource({
  name: 'user',
  endpoint: 'users',
  relations: {
      hasMany: {
          post: {
              localField: 'posts',
              foreignKey: 'userId'
          }
      }
  }
});

DS.defineResource({
    name: 'post',
    endpoint: 'posts',
    relations: {
        belongsTo: {
            user: {
                parent: true,
                localKey: 'userId',
                localField: 'user'
            }
        },
        hasMany: {
            comment: {
                localField: 'comments',
                foreignKey: 'postId'
            }
        }
    }
});

DS.defineResource({
    name: 'comment',
    endpoint: 'comments',
    relations: {
        belongsTo: {
            post: {
                parent: true,
                localKey: 'postId',
                localField: 'post'
            }
        }
    }
});
var user = DS.get('user', 1);
/*
  Load all the users posts with DS.loadRelations
  Load all comments of each post with DS.loadRelations
*/

// Look for changes in user 1
$scope.$watch(function () {
    return DS.lastModified('user', 1);
}, function () {
    console.log("User 1 has changed");
});

// Load a comment which belongs to a post which in turn belongs to user 1
DS.find('comment',1,{bypassCache:true}); // This will trigger the change in user 1

This makes it very hard to know when user 1 is updated. lastModified should probably only change when its own properties are changed (name and additions/deletions to its posts collection).

@jmdobry
Copy link
Member

jmdobry commented Aug 26, 2014

This is a good point. Wasn't really an issue until it became easy to link up the relations.

@jmdobry jmdobry added this to the 1.0.0-beta.2 milestone Aug 26, 2014
@jmdobry jmdobry self-assigned this Aug 26, 2014
jmdobry added a commit that referenced this issue Aug 27, 2014
@jmdobry
Copy link
Member

jmdobry commented Aug 27, 2014

@Patrik-Lundqvist I think I might have fixed this. Can you try it again with the latest master?

@Patrik-Lundqvist
Copy link
Author

It now updates properly for object properties like when changing name for a user. But now there is no way of detecting addition/deletion changes for a collection property in an object, like the user posts collection.

It would be nice if one could watch direct collection changes with something like this:

$scope.$watch(function () {
    return DS.lastModified('user', 1, ['posts']);
}, function () {
    console.log("User 1 posts has changed");
});

DS.inject('post', {id:1, content: 'test', userId: 1},{linkInverse: true}); 
// This should log "User 1 posts has changed"

Or maybe just update lastModified for the user on additions/deletions in the posts collection

@jmdobry
Copy link
Member

jmdobry commented Aug 27, 2014

Unfortunately, DS.lastModified just returns the timestamp of the last time the object changed. It has no control over why the timestamp would change.

You could do:

$scope.$watch(function () {
  return DS.lastModified('user', 1) + DS.lastModified('post');
}, function () {
  console.log("User 1 or posts has changed");
});

@Patrik-Lundqvist
Copy link
Author

Yeah, I thought so. Well that might be another issue, this fix works as intended.

@jmdobry
Copy link
Member

jmdobry commented Aug 27, 2014

Okay

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