如何更正参考Redux Reducer中的规范化实体数据

时间:2017-03-23 23:25:05

标签: redux reducers normalizr

所有

我是redux减速机的新手,大多数答案都指向normalizr.js。举个例子:

const blogPosts = [
    {
        id : "post1",
        author : {username : "user1", name : "User 1"},
        body : "......",
        comments : [
            {
                id : "comment1",
                author : {username : "user2", name : "User 2"},
                comment : ".....",
            },
            {
                id : "comment2",
                author : {username : "user3", name : "User 3"},
                comment : ".....",
            }
        ]    
    },
    {
        id : "post2",
        author : {username : "user2", name : "User 2"},
        body : "......",
        comments : [
            {
                id : "comment3",
                author : {username : "user3", name : "User 3"},
                comment : ".....",
            },
            {
                id : "comment4",
                author : {username : "user1", name : "User 1"},
                comment : ".....",
            },
            {
                id : "comment5",
                author : {username : "user3", name : "User 3"},
                comment : ".....",
            }
        ]    
    }
    // and repeat many times
]

我将架构定义如下:

var authorSchm = new schema.Entity("authors", {}, {idAttribute: "username"});
var commentSchm = new schema.Entity("comments", {author:authorSchm})
var commentList = [commentSchm];
var postSchm = new schema.Entity("posts", {author:authorSchm, comments:commentList});
var postList = [postSchm];


var normalizedData = normalize(blogPosts, postList);
console.log(JSON.stringify(normalizedData, null, 4));

我得到的结果如下:

{
    "entities": {
        "authors": {
            "user1": {
                "username": "user1",
                "name": "User 1"
            },
            "user2": {
                "username": "user2",
                "name": "User 2"
            },
            "user3": {
                "username": "user3",
                "name": "User 3"
            }
        },
        "comments": {
            "comment1": {
                "id": "comment1",
                "author": "user2",
                "comment": "....."
            },
            "comment2": {
                "id": "comment2",
                "author": "user3",
                "comment": "....."
            },
            "comment3": {
                "id": "comment3",
                "author": "user3",
                "comment": "....."
            },
            "comment4": {
                "id": "comment4",
                "author": "user1",
                "comment": "....."
            },
            "comment5": {
                "id": "comment5",
                "author": "user3",
                "comment": "....."
            }
        },
        "posts": {
            "post1": {
                "id": "post1",
                "author": "user1",
                "body": "......",
                "comments": [
                    "comment1",
                    "comment2"
                ]
            },
            "post2": {
                "id": "post2",
                "author": "user2",
                "body": "......",
                "comments": [
                    "comment3",
                    "comment4",
                    "comment5"
                ]
            }
        }
    },
    "result": [
        "post1",
        "post2"
    ]
}

我想知道如何在Redux reducer和combineReducers中使用这个规范化数据? 并且我怎么知道我应该根据参考字符串获取下一级数据的实体(例如:当我得到“post1”时,我怎么知道我应该去帖子实体获取post1对象和post1对象内部,如何为那些comment1,comment2 ..)

执行此操作

1 个答案:

答案 0 :(得分:0)

对此的任何答案都将主要取决于您的具体应用和用例。有关通用和推荐的方法,请查看官方example。这是combineReducers的样子。