0

Trying to get to grips with normalizr, data structure and schema shown below:

const data = {
    "name": "abc",
    "outlet": [
        {
            "id": 1,
            "retailer": {
                "id": 1
            }
        },
        {
            "id": 2,
            "retailer": {
                "id": 1
            }
        }
    ]
}

const retailerSchema = new schema.Entity('retailers');

const outletSchema = new schema.Entity('outlets', {
    retailer: retailerSchema
});

const normalized = normalize(data, {
    retailers: [retailerSchema],
    outlets: [outletSchema]
})

Current result:

normalized.result == {name: "abc", outlets: [1, 2]}
normalized.entities.retailers == {1: {id: 1}}
normalized.entities.outlets == {1: {id: 1, retailer: 1}, 2: {id: 2, retailer: 1}}

Is it possible to add outlet references based on unique retailers? e.g.

normalized.result == {name: "abc", outlets: [1, 2], retailers: [1]}
normalized.entities.retailers == {1: {id: 1, outlets: [1, 2]}}
normalized.entities.outlets == {1: {id: 1, retailer: 1}, 2: {id: 2, retailer: 1}}
4

1 回答 1

0

是否可以根据独特的零售商添加网点参考?

并不真地。Normalizr 返回的result总是与输入数据的格式相同。由于您的 data 是{ name, outlet },因此这些是输出中唯一存在的键result

如果您在顶级对象(整个data结构)上有一个唯一标识符,则可以使用processStrategyforschema.Entity和 shuffle 东西,但这真的不推荐。

于 2017-03-30T20:58:19.110 回答