简体   繁体   English

在发布/订阅中使用session.get变量

[英]Using a session.get variable in Publish/Subscribe

I'm trying to display a filterable database of clients that only loads the content required. 我正在尝试显示仅可加载所需内容的客户端可过滤数据库。 I've got two variables (selected from dropdowns) that set two sessions (country_plugin and vertical_plugin). 我有两个变量(从下拉列表中选择) ,它们设置了两个会话(country_plugin和vertical_plugin)。 I then want to display the content that meets those requirements. 然后,我想显示满足那些要求的内容。

The code below works perfectly if you're not worried about autopublish, but I have no idea how to achieve the same with pub/sub. 如果您不担心自动发布,则下面的代码可以完美地工作,但是我不知道如何使用pub / sub实现相同的功能。

So, in short, how can I use session.get and if statements here? 因此,简而言之,如何在这里使用session.get和if语句?

filteredclients: function () {
            var clientVerticalPicked = Session.get('vertical_plugin');
        var clientCountryPicked = Session.get('country_plugin');
            if (Session.get("country_plugin") === "none"){
                    return Clients.find({dealVerticalLink: clientVerticalPicked}, {sort: {dealMRR: -1}, limit:10});
            } else if (Session.get("vertical_plugin") === "none"){
                    return Clients.find({dealCountry: clientCountryPicked}, {sort: {dealMRR: -1}, limit:10});
            } else {
                    return Clients.find({$and:[{dealVerticalLink: clientVerticalPicked},{dealCountry: clientCountryPicked}]}, {sort: {dealMRR: -1}, limit:10});
            }
        }

Session variables are only defined on the client, so you won't be able to access them from within the publish function. 会话变量仅在客户端上定义,因此您将无法从publish函数中访问它们。 You can do your subscribe in a Tracker.autorun on the client, and pass the Session variables into the publish function: 您可以在客户端的Tracker.autorun中进行订阅,然后将Session变量传递到publish函数中:

Tracker.autorun(function() {
    Meteor.subscribe('filteredclients', Session.get('vertical_plugin'), Session.get('country_plugin'))
});

And on the server: 在服务器上:

Meteor.publish('filteredclients', function(vertical_plugin, country_plugin) {
    if(country_plugin === "none") { return ... }
});

etc. Then your Clients.find() on the client should only contain the records you want. 等等。那么客户端上的Clients.find()应该只包含所需的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM