【问题标题】:How to get id from main Entity and wrap it into nested one in normalizr如何从主实体获取 id 并将其包装到 normalizr 中的嵌套实体中
【发布时间】:2018-08-15 12:40:18
【问题描述】:

您好,我的包装对象的规范化存在问题。

我有一个仪表板和布局对象的数据数组,其中包含响应式视图的断点。

想要将它们全部标准化为两个实体,我的意思是仪表板和布局。

{
   "dashboards":[
      {
         "id":1,
         "name":"First",
         "mode":"2",
         "layouts":{
            "lg":[
               {
                  "x":0,
                  "y":0,
                  "w":2,
                  "h":2,
                  "i":"sm1"
               },
               {
                  "x":2,
                  "y":0,
                  "w":2,
                  "h":2,
                  "i":"sm2"
               }
            ],
            "md":[
               {
                  "x":2,
                  "y":0,
                  "w":2,
                  "h":2,
                  "i":"sm2"
               }
            ]
         }
      }
   ]
}

我试着那样做。但是我无法从仪表板中获取密钥并将其放入布局中,因为它是一对一的关系。

const layouts = new schema.Entity('layouts');
const mode = new schema.Entity('modes');


const dashboards = new schema.Entity('dashboards', {
    layouts: layouts,
    mode: mode
});
const dashboardListSchema = new schema.Array(dashboards);

const normalizedData = normalize(response, dashboardListSchema);

我的输出现在就像 dat:

仪表板:{ "1": { "id": 1, "name": "Główny", "mode": "2" } }

布局:{“未定义”:{“lg”:[{“x”:0,“y”:0,“w”:2,“h”:2,“i”:“sm1”}, {“x”:2,“y”:0,“w”:2,“h”:2,“i”:“sm2”}],“md”:[{“x”:2,“y” : 0, "w": 2, "h": 2, "i": "sm2" } ] } }

我想要仪表板的 id 而不是未定义的。谁能帮帮我?

【问题讨论】:

  • 发布您的解决方案作为答案并接受它,不要将 SOLVED 放入问题中。
  • 好的,已经完成了:)

标签: javascript vuex normalizr


【解决方案1】:

这是我的解决方案:)

const layouts = new schema.Entity('layouts', {}, {
    idAttribute: (value, parent) => parent.id
});

【讨论】:

    猜你喜欢
    • 2017-05-27
    • 2016-05-07
    • 2013-11-16
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2014-03-29
    相关资源
    最近更新 更多