我有一个出版物,其范围取决于另一个集合的元素属性.基本上它在服务器上看起来像这样:
Meteor.publish('kids', function (parent) {
return Kids.find({ _id: { $in: parent.childrenIds } });
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中parent.childrenIds是一个数组,其中包含父项子项的所有子项的_id.这工作正常,直到我想要向父母添加一个新的孩子:
newKidId = Kids.insert({ name: 'Joe' });
Parents.update({ _id: parentId }, { $push: { childrenIds: newKidId } });
Run Code Online (Sandbox Code Playgroud)
这工作在服务器上的Kids集合(即,新的孩子加入),并更新父childrenIds与阵列newKidId.但它不会更新上述'kids'出版物(光标未更新/修改).因此,客户端的Kids集合不会更新(看起来更改将Kids在客户端上回滚).
当客户端刷新时,所有发布都会停止/重新启动,新的孩子(Joe)最终会发布到客户端.
有没有办法避免刷新客户端并强制重新发布Kids集合(理想情况下只将新的孩子Joe发送给客户端)?
在Meteor中经常被误解的一件事是服务器上没有反应性.动态描述需要由Deps.autorun客户端上的块处理.为此,首先确保在项目目录中使用此命令不包括autopublish包:
$ meteor remove autopublish
Run Code Online (Sandbox Code Playgroud)
其次,在客户端上设置一个自动运行块,如:
Meteor.startup(function(){
Meteor.subscribe('parents');
Deps.autorun(function() {
parent = Parents.findOne();
if (!parent) return;
Meteor.subscribe('kids', parent);
});
});
Run Code Online (Sandbox Code Playgroud)
这将在父对象更改时拆除并设置订阅.
您可以在https://gist.github.com/jagill/5473599上看到完整的工作示例.
| 归档时间: |
|
| 查看次数: |
2466 次 |
| 最近记录: |