首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用iron-router过滤集合

使用iron-router过滤集合
EN

Stack Overflow用户
提问于 2017-02-10 01:40:15
回答 2查看 103关注 0票数 0

我有一个使用{{#each Collection}}渲染我的收藏的模板,我使用iron-router来路由这个模板,如下所示:

代码语言:javascript
复制
Router.route('/home', function () {
this.render('home');
this.layout('header');
});

如何使用"books“filter按钮以便iron-router将过滤器应用于订阅,以便我只能看到我收藏的过滤版本,如下所示:

代码语言:javascript
复制
Collection.find({category: "books"});
EN

回答 2

Stack Overflow用户

发布于 2017-02-10 02:10:38

有许多方法可以实现这一点,但一种简单的方法是定义一个以类别为参数的子路由:

代码语言:javascript
复制
Router.route('/collections/:category',{
  name: 'collections',
  layoutTemplate: 'header',
  data(){
    return Collections.find({category: this.params.category});
  }
});

然后你的按钮代码就可以做Router.go('/collections/books')了,但是你现在可以有多个按钮,每个按钮都绑定到不同的类别。

票数 0
EN

Stack Overflow用户

发布于 2017-02-12 08:35:27

按照这里的建议,https://guide.meteor.com/data-loading.html#organizing-subscriptions,你可以在模板中管理你的订阅,而不是在路线中。所以你可以这样做:

代码语言:javascript
复制
import {ReactiveDict} from 'meteor/reactive-dict';
import {Template} from 'meteor/templating';

Template.home.onCreated(function(){
  var that = this;
  that.state = new ReactiveDict();
  //that.state.set('selected-category',YOUR_DEFAULT_CATEGORY);
  that.autorun(function(){
    that.subscribe('subscription-name',that.state.get('selected-category'));
  });
});

Template.home.events({
  'click .js-category':function(e,tpl){
    let category = tpl.$(e.currentTarget).data('category');
    tpl.state.set('selected-category',category); 
  }
}); 

在您的模板中:

代码语言:javascript
复制
<button type="button" data-category="books" class="js-category">Books</button>

希望这将帮助您找到适合您的解决方案

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42143387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档