在Ember数据中有很多关系找不到任何关系

时间:2013-12-16 18:16:37

标签: ember.js ember-data

我正在使用Ember Data 0.14。

我有这些数据:

{
  "product": {
    "id": 185588,
    "name": "USB MIDI Adapter"
  },
  "images": [
    {
      "url": "google.com",
      "product_id": 185588
    },
    {
      "url": "google.com2",
      "product_id": 185588
    }
  ]
}

这些模型:

App.Image = DS.Model.extend({
    url: DS.attr('string'),
    product_id: DS.belongsTo('App.Product')
});

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

DS.RESTAdapter.map('App.Product', {
  images: { embedded: 'always' }
});

但我无法得到工作关系,产品上的“图像”是空的。这不是它应该如何工作吗?

2 个答案:

答案 0 :(得分:2)

您需要将数据嵌入images数组:

{
  "product": {
    "id": 185588,
    "name": "USB MIDI Adapter",
    "images": [{
      "id": 1,
      "url": "google.com",
      "product_id": 185588
    },
    {
      "id": 2,
      "url": "google.com2",
      "product_id": 185588
    }]
  }          
}

http://jsfiddle.net/marciojunior/TEgK7/(使用Ember Data 0.14)

答案 1 :(得分:1)

您错过了产品的映射,嵌入式暂时因问题而被禁用。 https://github.com/emberjs/data/blob/master/TRANSITION.md

{
  "product": {
    "id": 185588,
    "name": "USB MIDI Adapter",
    "images":["imageid1", "imageid2"....]
  },
  "images": [
    {
      "id": "imageid1",
      "url": "google.com",
      "product_id": 185588
    },
    {
      "id": "imageid2",
      "url": "google.com2",
      "product_id": 185588
    }
  ]
}

http://emberjs.jsbin.com/OxIDiVU/40/edit