Sideload使用EmberData列出“belongsTo”对象

时间:2013-07-31 20:38:59

标签: ember.js ember-data

我有3个emberData模型:

App.Product = DS.Model.extend({
  page_title: DS.attr('string'),
  shop: DS.belongsTo('App.Shop'),
  user: DS.belongsTo('App.User')
});

App.Shop = DS.Model.extend({
  name: DS.attr('string'),
});

App.User = DS.Model.extend({
  name: DS.attr('string')
});

,JSON数据如下所示:

{
  products: [
    {
      id: "1",
      page_title: "Product 1",
      user_id: "1",
      shop_id: "1",
    },
    {
      id: "2",
      page_title: "Product 2",
      user_id: "2",
      shop_id: "1",
    }
  ],
  users: [
    {
      id: "1",
      name: "User 1"
    },
    {
      id: "2",
      name: "User 2"
    }
  ],
  shops: [
    {
      id: "1",
      name: "Shop 1"
    }
  ]
}

但是当我加载数据时出现以下错误:

Assertion failed: Your server returned a hash with the key shops but you have no mapping for it 

1 个答案:

答案 0 :(得分:7)

好的,文档非常不清楚,当你有一个belongsTo关系时,sideload的密钥必须是单数而不是复数,即使它是一个列表。所以JSON必须如下所示:

{
  products: [
    {
      id: "1",
      page_title: "Product 1",
      user_id: "1",
      shop_id: "1",
    },
    {
      id: "2",
      page_title: "Product 2",
      user_id: "2",
      shop_id: "1",
    }
  ],
  user: [
    {
      id: "1",
      name: "User 1"
    },
    {
      id: "2",
      name: "User 2"
    }
  ],
  shop: [
    {
      id: "1",
      name: "Shop 1"
    }
  ]
}