1
votes

I'm building an adapter to wrap the Keen.io API, so far I've been able to successfully load the projects resource, however the returned object looks like this:

{
  partners_url: "/3.0/projects/<ID>/partners",
  name: "Project Name",
  url: "/3.0/projects/<ID>",
  saved_queries: [ ],
  events_url: "/3.0/projects/<ID>/events",
  id: "<ID>",
  organization_id: "<ORG ID>",
  queries_url: "/3.0/projects/<ID>/queries",
  api_key: "<API KEY>",
  events: [
    {
      url: "/3.0/projects/<ID>/events/user_signup",
      name: "user_signup"
    },
    {
      url: "/3.0/projects/<ID>/events/user_converted",
      name: "user_converted"
    },
    {
      url: "/3.0/projects/<ID>/events/user_created_project",
      name: "user_created_project"
    }
  ]
}

Excluding a lot of cruft, Ember has no trouble mapping the name and id attributes using the RESTSerializer, but if I add an events relation to my Project model it blows up with:

Error while loading route: TypeError: Cannot set property 'store' of undefined
    at Ember.Object.extend.modelFor (http://localhost:3000/assets/ember-data.js?body=1:9813:23)
    at Ember.Object.extend.recordForId (http://localhost:3000/assets/ember-data.js?body=1:9266:21)
    at deserializeRecordId (http://localhost:3000/assets/ember-data.js?body=1:10197:27)
    at deserializeRecordIds (http://localhost:3000/assets/ember-data.js?body=1:10211:9)
    at http://localhost:3000/assets/ember-data.js?body=1:10177:11
    at http://localhost:3000/assets/ember-data.js?body=1:8518:20
    at http://localhost:3000/assets/ember.js?body=1:3404:16
    at Object.OrderedSet.forEach (http://localhost:3000/assets/ember.js?body=1:3247:10)
    at Object.Map.forEach (http://localhost:3000/assets/ember.js?body=1:3402:10)
    at Function.Model.reopenClass.eachRelationship (http://localhost:3000/assets/ember-data.js?body=1:8517:42) 

From my investigation this seems to be because it can't find the inverse relation to map an Event back to a Project because there's no parent ID.

Is it possible to create a relation in Ember Data to support this? Or is it possible to modify the Serializer to append a projectId to each event before loading?

I'm using Ember 1.5.0-beta.4 and Ember Data 1.0.0-beta.7+canary.f482da04.

1

1 Answers

1
votes

Assuming your Project model is setup the following way:

App.Project = DS.Model.extend({
  events: DS.hasMany('event');
});

You need to make sure that the JSON from your API is in a certain shape that Ember-Data expects.

{
  "project": {
    "id": 1,
    "events": ["1", "2"],
  },

  "events": [{
    "id": "1",
    "name": "foo"
  }, {
    "id": "2",
    "name": "bar"
  }]
}

You can, however, implement extractArrayin your model's serializer to transform the JSON from the server into something similar like the above example.

There's a working example and an explanation in the Ember docs.