发布/订阅标准问题 [英] Pub/Sub Criteria Issue
本文介绍了发布/订阅标准问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在客户端创建过滤机制.我只发布了未被删除的集合;{ 移除:false }
.然后在客户端上,我有时想添加第二个条件 {tag: 'Sports'}
.不幸的是,我的代码不起作用.我假设我的订阅参数会扩展发布的标准.也许我错了?
I'm trying to create a filtering mechanism on the client side. I've published only collections that have not been removed; { removed: false }
. Then on the client I sometimes want to add a second criteria {tag: 'Sports'}
. The code I have does not work, unfortunately. I assumed my arguments for the subscribe would extend the criteria of the publish. Maybe I am wrong?
服务器:
Meteor.publish('posts', function() {
return Posts.find({ removed: false });
});
客户:
Meteor.subscribe('posts', { tag: 'Sports' })
推荐答案
第一个解决方案
服务器:
Meteor.publish('posts', function(query) {
// if passed query is NULL, then query = {}
query = query || {};
// we need to be sure that always we filter by {removed : false}
query.removed = false;
return Posts.find(query);
});
客户:
Meteor.subscribe('posts',{tag:'Sports'});
第二种解决方案
您的发布功能很好,您正在客户端进行第二次过滤.
Second solution
Your publish function is fine, second filtering you are doing on client side.
客户:
Meteor.subscribe('posts');
第二个条件可以在模板助手中使用:
The second criteria can be used inside Template helper:
Template.template_name.posts = function(){
return Posts.find({tag:'Sports'});
}
你这样使用它:
<template name="posts">
{{ posts }}
</template>
这篇关于发布/订阅标准问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文