EmberJS:我应该如何定义高度嵌入的JSON关系?

时间:2017-12-06 23:28:07

标签: json ember.js

我一直在努力使用EmberData并使用EmbeddedRecordsMixin来定义我正在使用的特定模型关系。我完全可以控制服务器和Ember应用程序,因此我知道我可以更改格式以使其工作,但我不知道正确的方法来获得我想要的东西。

我有一个名为膳食计划的模型,其中包含一个嵌入式配方 ID数组,我想通过Ember的hasMany功能自动加载它。

以下是我的服务器返回的数据结构:

meal_plan: {
      week: {
        1: { day: {
            0: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            1: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            2: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            3: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            4: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            5: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            6: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] }
          } },
        2: { day: {
            0: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            1: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            2: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            3: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            4: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            5: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] },
            6: { breakfast: [Number], lunch: [Number], dinner: [Number], snack: [Number] }
          } },
      }
}

以下是我希望这种关系在Ember中的样子:

week: {
        1: { day: {
            0: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            1: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            2: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            3: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            4: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            5: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            6: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
        } },
        2: { day: {
            0: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            1: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            2: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            3: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            4: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            5: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
            6: { breakfast: DS.hasMany('recipe'), lunch: DS.hasMany('recipe'), dinner: DS.hasMany('recipe'), snack: DS.hasMany('recipe') },
        } },
}

这个 hasMany 关系显然不起作用,因为它没有在顶层定义。我的食谱模型很简单:

export default DS.Model.extend({
  instructions: DS.attr('string'),
  name: DS.attr('string'),
  cooking_time: DS.attr('string'),
  recipe_yield: DS.attr('string'),
  featured_image: DS.attr('string'),
  ingredients: DS.attr(),
  program_phase: DS.attr(),
  meal_types: DS.attr()
});

此结构的目标是显示用户膳食计划的给定周的食谱列表,例如:

mealplan.week["1"].day["0"].breakfast
mealplan.week["1"].day["0"].lunch
mealplan.week["1"].day["0"].dinner
mealplan.week["1"].day["0"].snack

将显示我的食谱模型。

主要是我对如何使用我需要的结构正确嵌入配方ID 感到困惑。在阅读了JSON-API规范并查看其关系对象规范之后,我认为我的数据不能根据正确的规范进行格式化。就像我在开始时说的那样,我可以完全控制服务器,因此我可以更改数据结构,以便更好地符合JSON-API规范,如果这是我需要做的事情。

0 个答案:

没有答案