Ember,Ember-data和Rails关系错误:“无法读取未定义的属性'typeKey'”

时间:2014-08-11 16:11:08

标签: ruby-on-rails ember.js ember-data

我将Ember连接到一个提供一些JSON的Rails API。

我试图让关系有效(product hasMany images),但它一直给我这个错误:

Cannot read property 'typeKey' of undefined

我的模特:

App.Product = DS.Model.extend({
  images: DS.hasMany('image'),
  title: DS.attr('string'),
});


App.Image = DS.Model.extend({
  product: DS.belongsTo('product')
});

Rails将json渲染为:

{
  "products":[
    {
      "id": 1,
      "title": "product title",
      "images":[
        {
          "id": 1,
          "urls":
          {
            "thumb":"http://domain.com/thumb/image.jpg",
            "original":"http://domain.com/original/image.jpg"
          }
        }
      ]
    }
  ]
}

2 个答案:

答案 0 :(得分:2)

原来我需要" sideload"我在Rails中的图像,所以JSON成了:

{
  "products":[
    {
      "id": 1,
      "title": "product title",
      "image_ids": [1]
    }
  ],
  "images":[
    {
      "id": 1,
      "urls":
      {
        "thumb":"http://domain.com/thumb/image.jpg",
        "original":"http://domain.com/original/image.jpg"
      }
    }
  ]
}

滑轨' ProductSerializer:

class ProductSerializer < ActiveModel::Serializer

  embed :ids, :include => true

  attributes :id, :title

  has_many :images

  methods :image_urls

end

答案 1 :(得分:1)

您的示例中似乎使用了嵌入式JSON。您需要使用EmbeddedRecordsMixin https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/embedded_records_mixin.js并设置相应的标记以将images标记为嵌入