Ember.js 用余烬数据构造模型

Ember.js 用余烬数据构造模型,ember.js,ember-data,Ember.js,Ember Data,我已经开始使用余烬数据,在开始使用时遇到了一些问题。如果我的配料json结构是: [ { "name":"flax seed", "retailer":"www.retailer.com", "nutrient_info":[ { "type":"vitamin A", "amount":"50mg" }, { "type":"c

我已经开始使用余烬数据,在开始使用时遇到了一些问题。如果我的配料json结构是:

[
   {
      "name":"flax seed",
      "retailer":"www.retailer.com",
      "nutrient_info":[
         {
            "type":"vitamin A",
            "amount":"50mg"
         },
         {
            "type":"calcium",
            "amount":"30mg"
         }
      ]
   },
   {
      "name":"soy milk",
      "retailer":"www.retailer-two.com",
      "nutrient_info":[
         {
            "type":"vitamin D",
            "amount":"500mg"
         },
         {
            "type":"niacin",
            "amount":"5000mg"
         }
      ]
   },
   { other ingredients... }
]
我想这就是我定义模型的方式:

var attr = DS.attr,
    hasMany = DS.hasMany,
    belongsTo = DS.belongsTo

App.Ingredients = DS.Model.extend({
    // id: attr('number'),  // don't include id in model?
    name: attr('string'),
    retailer: attr('string'),
    nutrientinfo: hasMany('nutrients')
})

App.Nutrients = DS.Model.extend({
    type: attr('string'),
    amount: attr('string'),

    ingredient: belongsTo('ingredients')
})
服务器负载应该是什么样子,我需要定制REST适配器吗?我是否需要在模型中定义配料
id:attr()


感谢您在澄清这些概念方面提供的任何帮助。

通常,模型定义是单一的(此外,我将nutrientinfo更改为nutrient\u info):

格式需要如下(从端点或使用序列化程序)

下面是一个使用序列化程序和json的示例,我必须手动分配ID(尽管这是无效的,但您应该发送下ID或使用UUID),但这应该让您了解如何使用序列化程序:

App.IngredientSerializer = DS.RESTSerializer.extend({
  extractArray: function(store, type, payload, id, requestType)   {
    var ingredients = payload,
        nutrientId = 0,
        ingredientId = 0,
        ids = [],
        nutrients = [];

    ingredients.forEach(function(ing) {
      ing.id = ingredientId++;
      var nInfo = ing.nutrient_info,
          nIds = [];

      nInfo.forEach(function(n){
        n.id = nutrientId++;
        n.ingredient = ing.id;
        nIds.push(n.id);
        nutrients.push(n);
      });
      ing.nutrient_info = nIds;
    });

    payload = {ingredients:ingredients, nutrients:nutrients};

    return this._super(store, type, payload, id, requestType);
  }
});

因此,为了澄清问题,服务器请求的JSON不需要分别拆分为
成分
营养素
,因为序列化程序会这样做,对吗?是的,默认情况下序列化程序不会这样做。以下是一些很好的文档,直到它推出测试版:感谢您的解释,+1后续:我删除了
ing.id=ingredientId++
part,并为每个成分添加了一个唯一的ID,以便从JSON中传递。对营养素做同样的事情,给每个人一个独特的id,是否有益?
{
  // Ingredient records
  ingredients:[
    {
      id:1,
      "name":"flax seed",
      "retailer":"www.retailer.com",
      "nutrient_info":[1,2]
    },
    {
      id:2,
      "name":"soy milk",
      "retailer":"www.retailer-two.com",
      "nutrient_info":[3,4]
    },
    { other ingredients... }
  ],

  // Nutrient records
  nutrients: [
    {
      id:1,
      "type":"vitamin A",
      "amount":"50mg",
      ingredient:1
    },
    {
       id:2,
       "type":"calcium",
       "amount":"30mg",
      ingredient:1
    },
    {
       id:3,
       "type":"vitamin D",
       "amount":"500mg",
      ingredient:2
    },
    {
       id:4,
       "type":"niacin",
       "amount":"5000mg",
      ingredient:2
    }
  ]
}
App.IngredientSerializer = DS.RESTSerializer.extend({
  extractArray: function(store, type, payload, id, requestType)   {
    var ingredients = payload,
        nutrientId = 0,
        ingredientId = 0,
        ids = [],
        nutrients = [];

    ingredients.forEach(function(ing) {
      ing.id = ingredientId++;
      var nInfo = ing.nutrient_info,
          nIds = [];

      nInfo.forEach(function(n){
        n.id = nutrientId++;
        n.ingredient = ing.id;
        nIds.push(n.id);
        nutrients.push(n);
      });
      ing.nutrient_info = nIds;
    });

    payload = {ingredients:ingredients, nutrients:nutrients};

    return this._super(store, type, payload, id, requestType);
  }
});