繁体   English   中英

Ember数据中的hasMany关系找不到任何关系

[英]hasMany relation in Ember data finds no relations

我正在使用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' }
});

但是我无法获得工作关系,产品上的“图片”为空。 这不是应该的工作方式吗?

您需要将数据嵌入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数据0.14)

您缺少产品的映射,由于暂时的问题,嵌入式已被禁用。 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM