lundi 31 août 2015

Save belongsTo change in model not part of Payload

I'm trying to save a change to a parent model on a child model in Ember to my server, but for some reason the REST Payload doesn't contain the belongsTo Relationship

My 2 models are defined like this:

parent.js

import DS from 'ember-data';

export default DS.Model.extend({
  parentName: DS.attr('string')
});

child.js

import DS from 'ember-data';

export default DS.Model.extend({
  childName: DS.attr('string'),
  parent: DS.belongsTo('parent')
});

when I change the belongsTo to a different parent for a childModel by editing and saving an existing record. Somehow my payload doesnt include the parent model.

For example.

I have a child like this:

{
    id: 1,
    parent: 1,
    childName: "Child 1"
}

and in code I do this:

childModel.set('parent', parentModel); // this is a different parentModel, one with id: 2

I would expect the payload to look something like this:

HTTP-PUT: http://server/child/1

{
    id: 1,
    parent: 2,
    childName: "Child 1"
}

however, in reality, the payload is this:

HTTP-PUT: http://server/child/1

{
    id: 1,
    childName: "Child 1"
}

What goes wrong here? Why is the parent relationship missing from the payload?

Some extra information:

  1. Ember v2.0.1
  2. Ember-data v2.0.0
  3. Relationships must be async: true (which is the default)
  4. I'm using a standard DS.JSONAPISerializer in combination with the DS.RestAdapter



Aucun commentaire:

Enregistrer un commentaire