2
votes

Working on an App with Ember RC6 and Ember-Data v0.13-54 along a custom server PHP API am running into some problems with Models relations (mainly many-to-many) and side loading.

The models in questions are:

App.Member = DS.Model.extend({
  apiToken: DS.attr('string'),
  apiTokenExpire: DS.attr('string'),
  favourites: DS.hasMany('App.Presentation')
});

App.Presentation = DS.Model.extend(
{
  title: DS.attr('string'),
  description: DS.attr('string'),
  date: DS.attr('date'),

  category: DS.belongsTo('App.Category'),
  tags: DS.hasMany('App.Tag'),
  employees: DS.hasMany('App.Member'),

  presentation: DS.belongsTo('App.File'),
  presenterNotes: DS.belongsTo('App.File'),
  cover: DS.belongsTo('App.Image')
});

To get the RESTAdapater to send the relation with the Member model I have:

App.APIRESTAdapter.map('App.Member', {
  favourites: {embedded: 'always'}
});

When loading /members/1 the server returns:

{
  "member": {
    "api_token": "9fa236ad58726584d7b61bcf94b4dda37cbd3a24",
    "api_token_expire": "1383832335",
    "id": 1,
    "favourite_ids": [ 3 ],
    "group_ids": [ 2 ]
  },
  "presentations": [
    {
      "title": "That's a new one!",
      "category_id": 2,
      "id": 3,
      "tag_ids": [ 1 ],
      "employee_ids": [ 1 ]
    }
  ]
}

But if I log get('member.favourites').mapProperty('id') I get an empty array and none of the favourites are actually added to the Member model.

If I disable the embedding of favourites on the RESTAdapter, all works fine. I am just wondering if there is something that I am missing or if there is some issues with how the JSON response is formatted?


EDIT:

Done some digging and seems that the issue comes from the fact that the relation names (favourites, employees) is different from the model names (Member, Presentation) which are used when sideloading data. Weird, since rev. 12 https://github.com/emberjs/data/blob/master/BREAKING_CHANGES.md models should be Sideload by Type.

Doing some tests I've added a new many-to-many relation for those 2 models:

App.Member gets presentations: DS.hasMany('App.Presentation')

App.Presentation get members: DS.hasMany('App.Member')

The JSON returned by the server is exactly the same, and when logging get('member.presentations') I now get the list of presentations as it should.

At this point this looks like a bug to me, but maybe am missing something? I've tried mappings on the RESTAdapater for favourites and employees but that didn't help... Maybe there is some other Adapter config that could help?

1

1 Answers

1
votes

This isn't a sideloading issue but more a misunderstanding on my side about embedded data and what the configuration meant. Since the Adapter was configured with:

App.APIRESTAdapter.map('App.Member', {
  favourites: {embedded: 'always'}
});

The expected JSON response from the server is:

{
  "member": {
    "api_token": "b84fd204b37d3fa3cee8a2b2cac8bd4fd02635a1",
    "api_token_expire": "1384027367",
    "id": 1,
    "favourites": [
      {
        "title": "Some kind of title",
        "category_id": 1,
        "id": 2,
        "tag_ids": [ 1 , 2 ],
        "employee_ids": [ 1 ]
      }
    ]
  }
}

So "favourite_ids": [ X, X, X ] should have been "favourites": [ HASH, HASH, HASH ] when records are flagged as embedded.