【问题标题】:Structuring models with ember data使用 ember 数据构建模型
【发布时间】:2014-05-28 18:12:46
【问题描述】:

我已开始使用 ember 数据,但在开始使用时遇到了一些问题。如果我的 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()

感谢任何帮助澄清其中一些概念。

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    通常模型定义是单数的(另外我将 nutritioninfo 更改为 nutrient_info):

    App.Ingredient = DS.Model.extend({
        // id: attr('number'),  // don't include id in model?
        name: attr('string'),
        retailer: attr('string'),
        nutrient_info: hasMany('nutrient')
    })
    
    App.Nutrient = DS.Model.extend({
        type: attr('string'),
        amount: attr('string'),
        ingredient: belongsTo('ingredient')
    })
    

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

    {
      // 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
        }
      ]
    }
    

    这是一个使用序列化程序和您的 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);
      }
    });
    

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

    【讨论】:

    • 所以澄清一下,从服务器请求的 JSON 不需要单独拆分为 ingredientsnutrients,因为序列化程序会这样做,对吧?
    • 可以,序列化程序默认不这样做。
    • 在测试版发布之前,这里有一些很好的文档:github.com/emberjs/data/blob/master/TRANSITION.md
    • 感谢您的解释,+1
    • 跟进:我删除了ing.id = ingredientId++; 部分,并为每种成分添加了一个唯一的 ID,以便从 JSON 中传递下来。对营养素做同样的事情,并给每个人一个唯一的 id 是否有益?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    相关资源
    最近更新 更多