polyclick
polyclick

Reputation: 2703

Trouble consuming json rest data in ember-data

I'm trying to parse a json dataset in my ember data models but this throws an error in het console:

Uncaught TypeError: Cannot call method '_create' of undefined
    DS.Store.Ember.Object.extend.materializeRecord
    DS.Store.Ember.Object.extend.findByClientId

What am I doing wrong here?

This is the data I receive from the server:

{
  "newsitems": [
    {
      "date": "2013-02-10T15:00:00+01:00",
      "id": "1",
      "images": [
        {
          "id": "1",
          "value": "image.JPG"
        },
        {
          "id": "3",
          "value": "anotherimage.jpg"
        }
      ],
      "slug": "some-slug",
      "summary": "some summary",
      "text": "some text",
      "thumb": {
        "id": "2",
        "value": "someimage.JPG"
      },
      "title": "Some title",
      "type": "1",
      "videos": [
        {
          "id": "AEOpX8tmiUI",
          "value": "AEOpX8tmiUI"
        },
        {
          "id": "kxopViU98Xo",
          "value": "kxopViU98Xo"
        }
      ]
    }
  ]
}

These are my models:

App.NewsitemThumb = DS.Model.extend({
  value: DS.attr('string'),
  newsitem: DS.belongsTo('App.Newsitem')
});

App.NewsitemImage = DS.Model.extend({
  value: DS.attr('string'),
  newsitem: DS.belongsTo('App.Newsitem')
});

App.NewsitemVideo = DS.Model.extend({
  value: DS.attr('string'),
  newsitem: DS.belongsTo('App.Newsitem')
});

App.Newsitem = DS.Model.extend({
    slug: DS.attr('string'),
    type: DS.attr('string'),
    title: DS.attr('string'),
    summary: DS.attr('string'),
    text: DS.attr('string'),
    date: DS.attr('date'),

    thumb: DS.belongsTo('App.NewsitemThumb'),
    images: DS.hasMany('App.NewsitemImage'),
    videos: DS.hasMany('App.NewsitemVideo')
});

For the record, any suggestions for optimizing these models are welcome. It feels so weird to make 3 models for video, images and a thumb.

Upvotes: 1

Views: 886

Answers (1)

ahmacleod
ahmacleod

Reputation: 4310

According to this issue, this error pops up when you don't specify an explicit mapping for a hasMany relationship in your adapter.

Try defining your store as

App.Store = DS.Store.extend({
  revision: 11,
  adapter: DS.RESTAdapter.extend({
    serializer: DS.RESTSerializer.extend({
      init: function() {
        this._super();
        this.map("App.Newsitem", {
          images: { embedded: "load" },
          videos: { embedded: "load" }
        });
      }
    })          
  })
});

Upvotes: 1

Related Questions