Ember Data 1.0.0 belongsTo Relationship in JSON

Chris G

I'm using the RESTAdaptor and Ember-Data 1.0.0-beta-2. I'm following the guidance in the Ember docs http://emberjs.com/guides/models/connecting-to-an-http-server/ about how the JSON returned from the server should be formatted for belongsTo relationships - but the related object is not being loaded. The Ember Inspector in Chrome is always showing the relationship as null.

The format of my JSON from the server is as follows:

{
"danceStyle": {
    "id": 2,
    "name": "Balboa",
    "partnered": true,
    "_links": {
        "parent": "/danceStyles/1"
    }
  }
}

And my Model definition is:

var attr = DS.attr;
var belongsTo = DS.belongsTo;
var hasMany = DS.hasMany;

App.DanceStyle = DS.Model.extend({
object: attr('string'),
name: attr('string'),
partnered: attr('boolean'),
parent: belongsTo('danceStyle', { inverse: 'children', async: true }),
children: hasMany('danceStyle', { inverse: 'parent', async: true })
});

The format of my JSON matches up to the format that the Ember docs say I should be using, so I'm banging my head against the wall trying to work out what's wrong! I've tried other ways of including the relationship within the JSON but with no results.

Jeremy Green

I'm not sure that _links is used for relationships. (If so it is not well documented at all...)

Since you're using async:true you should be able to use JSON like this :

{
  "danceStyle": {
    "id": 2,
    "name": "Balboa",
    "partnered": true,
    "parent" : 1,
    "children" : [3,4]
  }
}

At that point, if you had loaded that model and then called .children() Ember Data would send a request like /danceStyles?ids[]=3,ids[]=4. Then your server should return :

{
  "danceStyles" : [
    {"id":3,...},
    {"id":4,...}
  ]
}

Alternatively, you can use "side loading" to deliver the children and parent along with the main model.

{
  "danceStyle": {
    "id": 2,
    "name": "Balboa",
    "partnered": true,
    "parent" : 1,
    "children" : [3,4]
  },
  "danceStyles" : [
    {"id":1,...},
    {"id":3,...},
    {"id":4,...}
  ]
}

I haven't actually dealt with side loading for self referential tree type models, but I know it works for an arrangement like Blog -> Post -> Comment.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ember Data 1.0.0 belongsTo Relationship in JSON

From Dev

ember data only with a belongsto relationship

From Dev

Nullable belongsTo relationship in ember-data?

From Dev

Ember Data. Save model with "belongsTo" relationship

From Dev

Ember Data embedded belongsTo relationship returning null

From Dev

Ember Data belongsTo Association (JSON format?)

From Dev

Properly create record with Ember Data that has belongsTo relationship

From Dev

Ember Data belongsTo async relationship omitted from createRecord() save() serialization

From Dev

Properly create record with Ember Data that has belongsTo relationship

From Dev

Ember Data belongsTo async relationship omitted from createRecord() save() serialization

From Dev

Ember not persisting belongsTo data

From Dev

Ember and Sails Create/Save Relationship (BelongsTo/HasMany)

From Dev

Ember belongsTo relationship passing as nil to server on saving

From Dev

Entity Framework 1 to 0 relationship

From Dev

ember-data belongsTo not working

From Dev

Ember data - set belongsTo to null

From Dev

ember-data hasMany relationship using json

From Dev

Defining a 1: 0 ...1 relationship in sql command

From Dev

EF CodeFirst bidirectional 1 : 0 or 1 relationship

From Dev

The relationship '{0}' was not loaded because the type '{1}' is not available

From Dev

The relationship '{0}' was not loaded because the type '{1}' is not available

From Dev

Relationship between LR(0), LL(0), LALR(1), etc?

From Dev

Getting data from Auth0/facebook JSON response with Ember 2.x

From Dev

ember / ember-data belongsTo and hasMany not updating as expected

From Dev

Accessing nested hasMany relationship in Ember / Ember Data

From Dev

Accessing nested hasMany relationship in Ember / Ember Data

From Dev

Ember data stripping hasMany relationship

From Dev

polymorphic relationship in ember-data

From Dev

HasMany Polymorphic Relationship In Ember Data

Related Related

  1. 1

    Ember Data 1.0.0 belongsTo Relationship in JSON

  2. 2

    ember data only with a belongsto relationship

  3. 3

    Nullable belongsTo relationship in ember-data?

  4. 4

    Ember Data. Save model with "belongsTo" relationship

  5. 5

    Ember Data embedded belongsTo relationship returning null

  6. 6

    Ember Data belongsTo Association (JSON format?)

  7. 7

    Properly create record with Ember Data that has belongsTo relationship

  8. 8

    Ember Data belongsTo async relationship omitted from createRecord() save() serialization

  9. 9

    Properly create record with Ember Data that has belongsTo relationship

  10. 10

    Ember Data belongsTo async relationship omitted from createRecord() save() serialization

  11. 11

    Ember not persisting belongsTo data

  12. 12

    Ember and Sails Create/Save Relationship (BelongsTo/HasMany)

  13. 13

    Ember belongsTo relationship passing as nil to server on saving

  14. 14

    Entity Framework 1 to 0 relationship

  15. 15

    ember-data belongsTo not working

  16. 16

    Ember data - set belongsTo to null

  17. 17

    ember-data hasMany relationship using json

  18. 18

    Defining a 1: 0 ...1 relationship in sql command

  19. 19

    EF CodeFirst bidirectional 1 : 0 or 1 relationship

  20. 20

    The relationship '{0}' was not loaded because the type '{1}' is not available

  21. 21

    The relationship '{0}' was not loaded because the type '{1}' is not available

  22. 22

    Relationship between LR(0), LL(0), LALR(1), etc?

  23. 23

    Getting data from Auth0/facebook JSON response with Ember 2.x

  24. 24

    ember / ember-data belongsTo and hasMany not updating as expected

  25. 25

    Accessing nested hasMany relationship in Ember / Ember Data

  26. 26

    Accessing nested hasMany relationship in Ember / Ember Data

  27. 27

    Ember data stripping hasMany relationship

  28. 28

    polymorphic relationship in ember-data

  29. 29

    HasMany Polymorphic Relationship In Ember Data

HotTag

Archive