4

我有 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 
4

1 回答 1

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"
    }
  ]
}
于 2013-07-31T21:06:05.257 回答