【发布时间】:2015-06-17 20:41:03
【问题描述】:
想象一下Quora。
[
{
type: "question",
answers: [
{
type: "answer",
upvotes: [
{
type: "upvote"
}
/* more upvotes */
],
comments [
{
type: "comment"
}
/* more comments */
]
}
/* more answers */
]
}
/* more questions */
]
我肯定会有QuestionsStore 之类的东西。但是对于所有子实体,我不确定如何处理它们。来自Backbone,我认为每个答案都应该有UpvotesStore 和CommentsStore,组件将从这些商店获取数据并订阅它们的更新。据我了解 Flux,“子”/关系存储有点不常见。
当每个组件都订阅来自 QuestionsStore 的更新时,会导致类似:
/* in CommentsComponent */
onUpdate: function() {
this.setState({
comments: QuestionsStore.getComments({questionId: 1, answerId: 1});
});
}
或更极端:
/* in CommentComponent */
onUpdate: function() {
this.setState(QuestionsStore.getComment({questionId: 1, answerId: 1, commentId: 1}));
}
由于关系数据存在于树结构中,因此每个组件都需要知道所有“父”ID,以便能够从QuestionsStore 查询它们的数据。我觉得这有点奇怪。
那么处理关系(一对多)数据结构的最佳 Flux 模式是什么?
【问题讨论】:
-
您可以让您的 API 返回您需要的任何数据,也可以采用基于承诺的结构,其中每个承诺都会解析它需要的数据。
-
还有这个,同一个问题:stackoverflow.com/questions/31641466/…
标签: javascript reactjs reactjs-flux flux