Charles
Charles

Reputation: 11768

EmberJS - Relationship not remotly pushed

I have two model with a one to one relation. I only need one direction of the relationship.

App.A = DS.Model.extend({
    b: DS.belongsTo('b', { async: true }),
});

App.B = DS.Model.extend({
    name: DS.attr('string'),
});

Model A is load from the server. Model B is manually pushed using store.pushPayload.

If Model B is loaded before Model A, everything is fine and {{a.b.name}} works correctly inside the template. But when Model A is loaded before Model B, then the name is empty (the whole relation is not set actually).

How can I tell model A that model B was loaded?

Upvotes: 0

Views: 57

Answers (1)

Igor
Igor

Reputation: 1588

Ember will automatically request your B model, if the relationship in model A exsists. Which ember/ember-data are you using? Are you using REST or JSONAPI adapters?

Your server response for model A needs to have the data about the relationship for model B.

i.e. in a JSON API response would look like this

"data": {
"type": "A",
"id": "1",
"attributes": {
  "some-attribute": "JSON API paints my bikeshed!"
},
"relationships": {
  "b": {
    "data": {
      "id": "123",
      "type": "b"
    },
  }
}

Upvotes: 2

Related Questions