2016-02-18 40 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我添加了一些有用的示例 –