ember-model icon indicating copy to clipboard operation
ember-model copied to clipboard

Add support for remote hasMany relationships

Open lagartoflojo opened this issue 11 years ago • 2 comments

Currently, it's expected for the server to deliver associations either with the embedded objects as an array of ids.

A third, unimplemented, case would be to load the associations using the parent's id. Something like:

parent.get('children')
# Results in
# GET /children?parent_id=XXX

Currently, this may be done as:

App.Parent = Ember.Model
  children: (->
    SC.Child.find parent_id: @get('id')
  ).property()

but there's no way of adding new children to the array.

Wouldn't it be useful if the hasMany macro accepted an option to load the associated items this way? New children could then be added with children.pushObject.

lagartoflojo avatar Aug 20 '13 23:08 lagartoflojo

I call this the "remote" hasMany case. It would be nice to have support for this baked in. Right now I suggest using a computed property that returns the result of a findQuery. You should be able to manipulate the returned RecordArray, but it's not a HasManyArray, which means it won't behave the exact same.

Would love if someone would work on tackling remote hasMany support - but it has to be generic, which means that you need to have a mechanism for generating URLs for fetching the hasMany.

ebryn avatar Aug 21 '13 19:08 ebryn

I just made a fork with partial support for this feature. Children are loaded by separate request with parents id, like "/rest/parent/1/children". This can be easyly changed in Adapter to "rest/children?parent_id=1". Children can be marked for deletion and actualy deleted by calling "save()" https://github.com/asquet/ember-model

asquet avatar Aug 22 '13 10:08 asquet