Meteor:在不使用自动发布的情况下创建搜索(过滤器)

时间:2016-05-03 14:31:42

标签: meteor

我的项目中有一个搜索工具,我需要能够在不使用 autopublish 的情况下发布结果。

HTML

Template.search.events({
  "submit #searchform": function (e) {
    e.preventDefault();
    Session.set("kategori", e.target.text.value);
  }
});

Template.search.helpers({
  profil: function() {
    return Profil.find({
      kategori: Session.get('kategori'),
    });
  }
});

JS:

{{1}}

1 个答案:

答案 0 :(得分:1)

您只需订阅即可过滤出版物:

客户端:

Template.search.events({
  "submit #searchform": function (e) {
    e.preventDefault();
    Session.set("kategori", e.target.text.value);
    Meteor.subscribe('profiles',Session.get('kategori'));
  }
});

服务器:

Meteor.publish('profiles',function(kategori){
  return Profil.find({ kategori: kategori });
});

如果您没有对同一个集合的任何其他订阅,您也可以简化您的帮助:

Template.search.helpers({
    profil: function() {
      return Profil.find();
    }
});

由于您的出版物将定义一组文件。

在实践中,尽管您通常在助手中使用与在出版物中相同的搜索,以避免出现其他出版物中的文档。