2

I'm using ember-app-kit with ember-data. I have the following two models created using ember-data. Node has a recursive relationship and Tag has a reference to the Node.

Node = DS.Model.extend({
  name: DS.attr('string'),
  createdAt: DS.attr('date'),
  tags: DS.hasMany('tag', {async: true}),
  children: DS.hasMany('node', {async: true, inverse: 'parent'}),
  parent: DS.belongsTo('node', {async: true, inverse: 'children'})
});

Tag = DS.Model.extend({
  name: DS.attr('string'),
  parent: DS.belongsTo('node', {async: true})
});

Whenever I try to reassign a tag to a different parent the changes do not persist.

NodeController = Ember.Controller.extend({
    actions: {
        update: function(newParentNode) {
           var node = model;
           node.set('parent', newParentNode);
           node.save().then(function(updatedNode){
              updatedNode.get('parent');// returns null
           }); //doesn't work
        }
    }
});

I found a similar question but the difference is that Tag also has a relationship to Node. Is it possible to update a parent-child relationship by only updating the child?

Update #1:

When examining the output from ember-data in the console I noticed that nothing about the parent child relationship is communicated on the save. The request payload to the backend is sent as the following:

{ "node":  { "name": "node4", "createdAt": null } }

Nothing about the parent/child relationship is transmitted. However if I reassign a tag to a different node the backend receives the following:

{"tag": {"name":"tag123", "parent":"66c8ec8c-3790-45b2-8669-e9581102376d"} }

I tried simplifying my node model by removing the inverses. This allowed me to reassign parents, but ember-data started to send the children relationship on the requests as the means of updating the relationship.

{ "node":  { 
             "name": "node4", 
             "createdAt": null, 
             "children": ["66c8ec8c-3790-45b2-8669-e9581102376d"] 
           } 
}

While this gets the job done, it is not ideal. As there could many children and sending the ids of every child will bloat the request payload. Is there no way for ember-data to send the updated info of child with its new parent relationship key? Also this change caused error messages to start being raised within Qunit.

You defined the 'parent' relationship on (subclass of DS.Model), but multiple possible inverse relationships of type (subclass of DS.Model) were found on (subclass of DS.Model)

KnownSubset
  • 139
  • 7

0 Answers0