这个问题建立在上一个问题的基础上(参见here https://stackoverflow.com/questions/16252077/how-to-deal-with-dynamic-subscriptions-in-meteor/).
动态订阅是使用以下代码设置的(对上一个问题稍作修改):
Meteor.startup(function(){
Meteor.subscribe('parents');
Deps.autorun(function() {
parent = Parents.findOne({ _id: Session.get('parentId') });
if (!parent) return;
Meteor.subscribe('kids', parent);
});
});
问题是服务器端必须信任parent
客户端传递的对象。理想情况下,人们只想通过_id
父对象的像这样:
Deps.autorun(function() {
parentId = Session.get('parentId');
if (!parentId) return;
Meteor.subscribe('kids', parentId);
});
但是,在这种情况下,动态订阅行为会中断(例如,kids
当父级的子级数组更新时,集合不会在客户端上更新)。
Why is Session.get('parentId')
反应性低于Parents.findOne({ _id: Session.get('parentId') })
,或与此有关Meteor.subscribe('kids', parent)
vs. Meteor.subscribe('kids', parentId)
?
正确编码的最佳模式是什么?
看起来您想要做的事情如下:
Deps.autorun(function() {
parent = Parents.findOne({ _id: Session.get('parentId') }, {fields: {_id: 1}});
if (!parent) return;
Meteor.subscribe('kids', parent._id);
});
然而,这仍然不完全安全。它只是检查Parents
集合以确保引用的 Session 变量在尝试订阅之前存在 - 这取决于parents
订阅。如果您希望它得到妥善保护,您将不希望派任何父母过来parent
订阅客户端(如果客户端不应该看到它们)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)