EmbeddedRecordsMixin导致嵌入式记录中出现TypeError

时间:2014-11-03 15:54:48

标签: ember.js ember-data

当我的记录嵌入数据时,我在使用DS.EmbeddedRecordsMixin时收到以下错误:

TypeError: Cannot read property 'typeKey' of undefined

我使用Ember CLI 0.1.2与Ember 1.7.0和Ember Data 1.0.0#beta11

我的适配器:

应用程序适配器 - /app/adapters/application.js(RestAdapter):

import DS from 'ember-data';
import config from '../config/environment';

export default DS.RESTAdapter.extend({
    namespace: config.APP.RestAdapterNamespace,
    host: config.APP.SERVER_LOCATION
});

有问题的适配器 - /app/adapters/screen.js(screenSlideGroups应嵌入):

import ApplicationAdapter from './application';
import DS from 'ember-data';

export default ApplicationAdapter.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        screenSlideGroups: { embedded: 'always' }
    }
});

型号:/app/models/screen.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  path: DS.attr('string'),
  screenSlideGroups: DS.hasMany('screen-slide-group')
});

从API返回的数据示例:

{
  "screen":[
     {
        "id":1,
        "name":"Weather",
        "path":"weather",
        "screenSlideGroups":[
           {
              "id":1,
              "screen":1,
              "slideGroup":1,
              "order":1
           }
        ],
        "lastUpdated":"2014-09-18T18:26:25.69"
     },
     {
        "id":2,
        "name":"Front Lobby",
        "path":"frontlobby",
        "screenSlideGroups":[

        ],
        "lastUpdated":"0001-01-01T00:00:00"
     }
  ]
}

我还尝试从嵌入式记录中删除screen,但是向后引用可能会搞砸它,但它并没有产生任何影响。据我所知,我创建的EmbeddedRecordsMixin适配器可能根本无法使用。

关于这里可能出错的任何想法?

1 个答案:

答案 0 :(得分:0)

原来我误读了文档,DS.EmbeddedRecordsMixin应该在Serializer上,而不是适配器。

正确的实施如下:

/app/serializers/screen.js:

import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        screenSlideGroups: { embedded: 'always' }
    }
});