Load all hasMany relationships before update model? Is there an other solution


#1

Hi,

at the moment if I would delete an comment at post, I have to load all hasMany relationships before. If all hasMany relationships loaded, than I can remove an comment form Post Object. Is there an other solution? It is really tricky to load all hasMany relations every times. if I add an object to an hasMany relationship.

App.Post = DS.Model.extend({
    reporter: DS.belongsTo('user'),
    comments: DS.hasMany('comment', { async: true }),
    currentUser: DS.belongsTo('user'),
    processors: DS.hasMany('user', { async: true }),
});

#2

I’m not sure to understand your use-case. Do you have a section in your application where all the posts generated by the user are displayed?


#3

If I understand you correctly then no, you need to retrieve the model before you can delete it if you’re sticking to “pure” ember-data. You can go around ember-data to trigger the delete on the back-end directly, though if you go that route you should probably first check whether the model is loaded. Maybe define a new method to do just that on your Store for it?


#4

ED will soon have lazy many arrays. You will be able to do

//This will not actually fetch all the records like it does now
post.get('comments').then(function(comments) {
  //this will fetch one comment 
  comments.objectAt(50).then(function(comment) {
     //this will remove it
    comments.removeObject(comment);
  });

}):

@locks started work on this here https://github.com/emberjs/data/pull/2192 Its one of the features thats on the top of my priority list, I’ll pick it up soon


#5

@terzicigor in this case what does the comments arrays is? And how does it would work in the case where I want to fetch all the comments on the relations?


#6

I have the same use case, I would delete an existing comment from, but have load all hasMany relations of my object before. So I have to load processors and comments before, booth promises should be resolved and than I can delete an comment object and do an save. If I don’t load the users before, the array is empty on save method.