0
votes

I'm new to Ember, and am having a problem I'm not seeing duplicated anywhere else so I'm sure I'm doing something silly. I have these models:

App.User = DS.Model.extend({
    username: DS.attr("string"),
    userId: DS.attr("number"),
    modules: DS.hasMany("App.Module")
});
App.Module = DS.Model.extend({
    moduleId: DS.attr("number"),
    name: DS.attr("string")
});

Note that my Module model is simply a container that a User can have a few of, and many users might share the same modules - they're actually represented by an Enum on the server. As such, the backend does not have a mapping of Module > Users, so I've left the DS.ownedBy or DS.hasMany out of App.Module. I have, however, tried my test with a "users: DS.hasMany('App.User')" in there as well, with the same result. If it turns out to be necessary, I could maintain such a mapping, but I have no other reason for doing so right now, so it would be a bit unfortunate to be forced to add such a table to my database.

I'm using Ember.Auth for authentication, and when my app loads up and I log in, the server requests (as expected):

users/nathan?authToken=<token>

The result is also as I think it should be, according to the ember data docs:

{
    "user": {
        "username": "nathan",
        "user_id": 1,
        "module_ids": [1,2]
    },
    "modules": [
        {"module_id": 1, "name": "Account Settings"},
        {"module_id": 2, "name": "Manage Websites"}
    ]
}

I'm then testing in the Chrome console with:

App.Auth.get("user").get("modules");

or

App.User.find("nathan").get("modules");

and in both cases, Ember makes a request to my server to get the data for Modules 1 and 2. If I repeat the same request, it doesn't go back to the server again, so it is storing the result properly that time, it's simply the sideload that it's ignoring.

I'm using ember-1.0.0-rc4 with ember-data-0.13.

2

2 Answers

1
votes

In your sideload response, module_id should be id.

(or you can configure ember-data to use module_id, but formatting the server response should be the easier way?)

--- edit for explanation ---

I'm not sure the original REST call is "working perfectly". Without the id. ember-data does see the two modules that your originally sideloaded, but it sees no id, so it does not know that they are modules 1 and 2 respectively. By default (changeable), ember-data expects the id to be called id; your module_id (and user_id) are just treated as regular properties.

On your next API call (to /modules?ids[]=1&ids[]=2 presumably), ember-data silently assumes that, since your requested two modules, and two modules came back, they should be the two that you requested. Try sending back this

{
    "modules": [
        {"module_id": 3, "name": "Foo module"},
        {"module_id": 4, "name": "Bar module"}
    ]
}

for the request /modules?ids[]=1&ids[]=2, you will still get your "successful" observation.

Try App.Module.find(1).get('name') with the module_id-response - you will get undefined; now try it with the id-response, and you will get Account settings as expected.

0
votes

Have you configured your RestAdapter to sideload modules?

Like this:

DS.RESTAdapter.configure('App.Module', {
  sideloadsAs: 'modules'
});