如何使用normalizr 2创建主/根模式?

时间:2017-03-10 18:00:40

标签: javascript normalizr

说我有以下架构和数据:

const user = new schema.Entity('users')
const video = new schema.Entity('videos', {
  user
})
const comment = new schema.Entity('comments', {
  video
})

// Sample Data
const users = [
  { id: 1, object: "User", name: "User 1" },
  { id: 2, object: "User", name: "User 2" },
]
const videos = [
  { id: 1, object: "Video", user: users[0] },
  { id: 2, object: "Video", user: users[1] },
]
const comments = [
  { id: 1, object: "Comment", video: videos[0] },
  { id: 2, object: "Comment", video: videos[0] },
]  

如何设置resourceresourceList架构来包装我的所有架构,以便在我的api调用中我只需要引用resourceresourceList,例如:

api.videos.list().then(
  // response.data is an array containing multiple videos
  response => normalize(response.data, resourceList)
) 
// entities: { videos: [...], users: [...] }

api.videos.retrieve(videoId).then(
  // response.data is an object containing a single video
  response => normalize(response.data, resource)
)

我试过这样做:

const resource = {
  user,
  video,
}

const resourceList = new schema.Array(resource, inferSchema)

function inferSchema({ object: objectName = '' }) {
  return objectName.charAt(0).toLowerCase() + objectName.slice(1)
}

但这似乎不起作用。

0 个答案:

没有答案