kurko / ember-json-api

JSON API adapter for Ember Data.
MIT License
254 stars 62 forks source link

Linked resources are sent as `null` on creation #28

Closed egeriis closed 9 years ago

egeriis commented 10 years ago

When I save a new model to be persisted on the server, the payload for relationships appears to be wrong. In this example options is a hasMany relationship:

{
  "surveys": [
    {
      "title": "abc",
      "description": "test",
      "end": "2014-09-11",
      "links": {
        "options": [
          null,
          null,
          null
        ]
      }
    }
  ]
}

Model for option looks like this:

module.exports = DS.Model.extend({
  title: DS.attr()
});
kurko commented 10 years ago

@egeriis thanks very much for opening all these issues.

Could you provide the code you're using to save the surveys? That way we can try to replicate the problem.

egeriis commented 10 years ago

In my save action I am simply calling save on the model from my controller; this.get('model').save().

The options are added through this action:

    addOption: function() {
      var opts = this.get('model').get('options');
      opts.pushObject(this.store.createRecord('option', {}));
    }

The title attribute are then bound in my template.

Is this sufficient?

egeriis commented 10 years ago

After investigating a bit, I can see that this occurs because it is new option instances that are not saved before the survey model. But I can also see from jsonapi.org that this should not necessarily be possible.

achan commented 9 years ago

@egerlis did you ever find a solution to this?

@kurko what are your thoughts about removing all hasMany attributes from createRecord? How would you even refer to an uncreated hasMany attribute? They do not have IDs yet... hence the nulls. I don't see how the current state of JSONAPI can accommodate this scenario without two transactions...

egeriis commented 9 years ago

@achan I believe what I ended up doing, was to save all options and wait for their promises to resolve before saving the parent model. I would prefer that ember-json-api could handle this automatically, though.

kurko commented 9 years ago

Yeah. Currently, the best strategy is to save one then the other. However, that's kinda bad: what if the second fails and then the first shouldn't be saved? You can't really do a unique transaction for both.

jonkoops commented 9 years ago

I am closing this issue due to lack of activity and the impending deprecation in favour of the official Ember Data adapter. If you think this is an issue that relates to Ember Data please report the issue there.