[英]Meteor React 2 Level down collection stopped being reactive
I have 1 project with one or more taskTrees. 我有1个项目,其中包含一个或多个taskTree。
For each taskTree, I have multiple tasks. 对于每个taskTree,我都有多个任务。
The problem is whenever I add new task, I don't see the task being added until I refresh the page. 问题是,每当我添加新任务时,直到刷新页面后,我才会看到正在添加的任务。
I am using Meteor React 1.2.1 with following packages 我正在将Meteor React 1.2.1与以下软件包一起使用
react
kadira:flow-router
meteortoys:allthings
kadira:react-layout
I also have 3 collections. 我也有3个收藏。
Projects = new Mongo.Collection("projects")
TaskTrees = new Mongo.Collection("task-trees")
Tasks = new Mongo.Collection("tasks")
My collection structure looks like this: 我的收藏结构如下:
**Projects**
_id
taskTrees: [taskTreeId]
**TaskTrees**
_id
tasks: [taskId]
**Tasks**
_id
title
I use FlowRouter to render ProjectPage 我使用FlowRouter呈现ProjectPage
FlowRouter.route('/project-page', {
action(params, queryParams) {
ReactLayout.render(ProjectPage)
}
})
This is my 3 react classes 这是我的3个反应班
ProjectPage = React.createClass({
mixins: [ReactMeteorData],
getMeteorData() {
Meteor.subscribe('project-page')
return {
taskTrees: TaskTrees.find().fetch()
}
},
renderTaskTrees() {
if(this.data.taskTrees) {
return this.data.taskTrees.map( (taskTree) => {
return <TaskTree
key={taskTree._id}
taskTreeId={taskTree._id} />
})
}
},
render() {
return(
<div>
RenderingTaskTree
{this.renderTaskTrees()}
</div>
)
}
})
TaskTree = React.createClass({
mixins: [ReactMeteorData],
getMeteorData() {
Meteor.subscribe('task-tree', this.props.taskTreeId)
return {
tasks: Tasks.find().fetch()
}
},
renderTasks() {
if(this.data.tasks) {
return this.data.tasks.map( (task) => {
return <Task
key={task._id}
title={task.title} />
})
}
},
render() {
return(
<div>{this.renderTasks()}</div>
)
}
})
Task = React.createClass({
render() {
return(
<div>
<div>Title: {this.props.title}</div>
</div>
)
}
})
This is my publish method in server folder 这是我在服务器文件夹中的发布方法
Meteor.publish('project-page', () => {
const project = Projects.findOne()
if(project.taskTrees) {
const taskTrees = TaskTrees.find({_id: {$in: project.taskTrees}})
return taskTrees
}
this.ready()
})
Meteor.publish('task-tree', (taskTreeId) => {
const taskTree = TaskTrees.findOne({_id: taskTreeId})
if(taskTree.tasks) {
const tasks = Tasks.find({_id: {$in: taskTree.tasks}})
return tasks
}
this.ready()
})
I think it makes more sense for you to put the subscribes you need from [ReactMeteorData]
all inside the ProjectPage
component, and then pass the reactive data as props
to its children. 我认为将[ReactMeteorData]
所需的订阅全部放入ProjectPage
组件内,然后将响应数据作为props
传递给它的子代,这更有意义。
Also, you should return the cursors directly in your publish functions, as mentioned here . 另外,您应该直接在发布函数中返回游标,如此处所述 。
So your first publish function would now look like this: 因此,您的第一个发布函数现在将如下所示:
if(project.taskTrees) {
return TaskTrees.find({_id: {$in: project.taskTrees}});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.