2016-02-18 39 views
1
import { normalize, Schema, arrayOf } from 'normalizr'; 

var ListA = [ 
    { 
     id:1, 
     text: "text1", 
     comments : [ 
      { 
       id: 232, 
       text: "asfasd" 
      }, 
      { 
       id: 333, 
       text: "abcsss" 
      } 
     ] 
    }, 
    {id:2, text:"text2", comments:[]}, 
    {id:3, text:"text3", comments:[]} 
    ] 

我想正常化这个简单的响应。我不确定我在做什么或者我没有理解normalizr文档。Normalizr - 不是预期的结果

const post = new Schema('posts'); 
// const comment = new Schema('comments'); 
// const collection = new Schema('collections'); 

// post.define({ 
// comments : comment, 
// collections : arrayOf(collection) 
// }); 
ListA = normalize(ListA, { 
    posts: arrayOf(post) 
}); 

console.log(ListA); 

这只会导致对“结果”对象的相同响应,而实体对象为空。有人可以帮帮我吗。首先,我正在努力使邮政正常化,然后再评论。但是我还没有能够跨越第一步。

回答

4

使用normalizr

1)归一化的简单对象
只是举例的几个例子

import { Schema, arrayOf } from 'normalizr'; 
const postSchema = new Schema('posts'); 

// simple post object 
var post = { 
    id:1, 
    title: "some title" 
}; 

console.log(normalize(post, postSchema)); 

结果将是

{ 
    "entities":{ 
     "posts":{ 
     "1":{ 
      "id":1, 
      "title":"some title" 
     } 
     } 
    }, 
    "result":1 
} 

2)归一化对象的阵列

import { Schema, arrayOf } from 'normalizr'; 
const postSchema = new Schema('posts'); 

// array of posts 
var posts = [ 
    { 
    id:1, 
    title: "foo" 
    }, 
    { 
    id:2, 
    title: "far" 
    }, 
    { 
    id:3, 
    title: "baz" 
    } 
]; 

console.log(normalize(posts, arrayOf(postSchema))); 

结果将是

{ 
    "entities":{ 
     "posts":{ 
     "1":{ 
      "id":1, 
      "title":"foo" 
     }, 
     "2":{ 
      "id":2, 
      "title":"far" 
     }, 
     "3":{ 
      "id":3, 
      "title":"baz" 
     } 
     } 
    }, 
    "result":[ 
     1, 
     2, 
     3 
    ] 
} 

3)归一化的复杂对象

const postSchema = new Schema('posts'); 
const authorSchema = new Schema('authors'); 
const commentSchema = new Schema('comments'); 

postSchema.define({ 
    author: authorSchema, 
    comments: arrayOf(commentSchema) 
}); 

// complex post object 
var post = { 
    id:1, 
    title: "foo", 
    author: { 
    id: 201, 
    name: "Bar Baz" 
    }, 
    comments: [ 
    { 
     id: 1002, 
     body: "Some content" 
    }, 
    { 
     id: 1003, 
     body: "Some content 1003" 
    }, 
    { 
     id: 1004, 
     body: "Some content 1004" 
    } 
    ] 
}; 

console.log(normalize(post, postSchema)); 

结果将是

{ 
    "entities":{ 
    "posts":{ 
     "1":{ 
     "id":1, 
      "title":"foo", 
      "author":201, 
      "comments":[ 
      1002, 
      1003, 
      1004 
     ] 
     } 
    }, 
    "authors":{ 
     "201":{ 
     "id":201, 
      "name":"Bar Baz" 
     } 
    }, 
    "comments":{ 
     "1002":{ 
     "id":1002, 
      "body":"Some content" 
     }, 
     "1003":{ 
     "id":1003, 
      "body":"Some content 1003" 
     }, 
     "1004":{ 
     "id":1004, 
      "body":"Some content 1004" 
     } 
    } 
    }, 
    "result":1 
} 

所以,你可以尝试

ListA = normalize(ListA, arrayOf(post)); 

,而不是

ListA = normalize(ListA, { 
    posts: arrayOf(post) 
}); 
+0

谢谢..它的工作。你能否详细解释它是如何工作的。就像iIalso想要标准化Comment一样。随着最近尝试进入Redux架构,除非正确理解,否则一切看起来都很混乱/魔幻。再次感谢。 – user98239820

+0

@ user98239820我添加了一些有用的示例 –