angular-restmod icon indicating copy to clipboard operation
angular-restmod copied to clipboard

would be awesome to include relations in encoded records

Open blackjid opened this issue 11 years ago • 6 comments

blackjid avatar Jul 23 '14 21:07 blackjid

This probable should happen only with hasMany and hasOne relations. By default I think data should be sent in a *_attributes property.

Proposed usage:

restmod.model({
  many: { hasMany: 'Other', append: 'create|update|true', appendAs:  }
});

There is also a special behaviour when removing objects from hasMany inline relations, some frameworks like rails expect a special property to be sent along with the destroyed object (_destroy: true). This also means that when an item is destroyed we should remove it from collection and keep a destroyed item stack to know what to report to the server, so $destroy should be overriden for that collection and it childs.

I'm not sure if the $save method should automatically save the nested records or should only do it if a special option is given:

parent.$save({ include: 'many' })

Any proposals?

iobaixas avatar Aug 18 '14 21:08 iobaixas

This is definitely needed. In batmanjs, if the model has a saveInline: true option set then it would automatically include the related record(s) when the parent was saved. I need this desperately right now, but I just started using this library. Please help!

jimmybaker avatar Aug 28 '14 05:08 jimmybaker

Ok, I'll give it priority when work on 1.1 starts, for now, this can be accomplished using a before-render hook:

restmod.model('/url', {
  others: { hasMany: 'Other' },

  '~before-render' : function(_raw) {
     _raw.others = this.others.$encode(); // this will not take care of destroyed items though..
  }
});

To handle destroyed items, you could add an after-remove hook to the relation collection that stores removed items in a collection's special property and then appends them to the raw data in the before-render hook above. If you need details on how to do this let me know.

iobaixas avatar Aug 28 '14 13:08 iobaixas

In the past I have left the deleted items in the collection with a flag of _destroy set to '1' then wherever I'm rendering items, I simply say ng-hide="item._destroy == '1'".

Why would we try to segregate them?

jimmybaker avatar Aug 28 '14 15:08 jimmybaker

I was just thinking about making it independent from view implementation, how does batmanjs handles it?

iobaixas avatar Aug 28 '14 15:08 iobaixas

They don't remove them either.

They have an update coming so that when you have a relation that is set to saveInline: true, then it will expect the relationships to be returned with the resource and it simply overwrites all the in-memory relationship records with the ones returned from the parent.

jimmybaker avatar Aug 28 '14 15:08 jimmybaker