繁体   English   中英

Meteor:如何通过会话变量过滤数据

[英]Meteor: How to filter data via a session variable

我有三个按钮来显示不同类型的信息

  1. 查看全部(即新闻和活动)
  2. 只有新闻
  3. 只有事件

状态 :仅过滤新闻或事件有效。 但如何看待两者?

我的问题是结合会话变量编写mongodb查询。 注释掉的线显示失败的方法。 在我失败的方法中,我试图将更多内容放入会话值(即添加单词类型)。 但是,这打破了所有疑问。

我的js文件:

Template.newsEvents.helpers({
  newsEventsData: function () {
    // return NewsEvents.find({Session.get('newsEventsView')}, {sort: {createdAt: -1}, limit: 3});
    return NewsEvents.find({type: Session.get('newsEventsView')}, {sort: {createdAt: -1}, limit: 3});
  }
});

Template.newsEvents.events({
    'click #js-seeAll': function (evt, tpl) {
        //Session.set('newsEventsView', '$or: [ { type: "news" }, { type: "event" } ]');

    },
    'click #js-seeNews': function (evt, tpl) {
        //Session.set('newsEventsView', 'type: "news"');
        Session.set('newsEventsView', 'news');
    },
    'click #js-seeEvents': function (evt, tpl) {
        //Session.set('newsEventsView', 'type: "event"');
        Session.set('newsEventsView', 'event');
    }
});

我的JSON文件:

{
    "_id" : "7sLK32LdoLv4NmtMJ",
    "title" : "3rd News",
    "description" : "Some News",
    "type" : "news",
    "createdAt" : ISODate("2016-01-18T11:23:36.412Z")
}

任何帮助赞赏。

使用$ in子句并为Session变量'newsEventsView'使用数组。 我个人使用它来根据消息类型过滤消息。 消息的类型将添加到存储为Session变量的数组中,然后传递给$ in子句。 如果用户不想看到某种类型,他们会单击一个更新Session变量的按钮。 您的代码如下所示:

Template.newsEvents.events({
    'click #js-seeAll': function (evt, tpl) {
        Session.set('newsEventsView', [ "news", "event" ]);
    }
});

Template.newsEvents.helpers({
    newsEventsData: function () {
        return NewsEvents.find({
            type: {
                $in: Session.get('newsEventsView')
            }, 
            {
                sort: {
                    createdAt: -1
                }, 
                limit: 3
            }
        });
    }
});

暂无
暂无

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

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