Meteor 使用iron路由器过滤集合
我有一个使用Meteor 使用iron路由器过滤集合,meteor,iron-router,Meteor,Iron Router,我有一个使用{{{each Collection}}呈现我的收藏的模板,我使用iron router按如下方式路由此模板: Router.route('/home', function () { this.render('home'); this.layout('header'); }); 我如何使用“books”过滤器按钮,以便将该过滤器应用于订阅,以便我只能看到我收藏的过滤版本,如下所示: Collection.find({category: "books"}); 有多种方法可以实现这一
{{{each Collection}}
呈现我的收藏的模板,我使用iron router按如下方式路由此模板:
Router.route('/home', function () {
this.render('home');
this.layout('header');
});
我如何使用“books”过滤器按钮,以便将该过滤器应用于订阅,以便我只能看到我收藏的过滤版本,如下所示:
Collection.find({category: "books"});
有多种方法可以实现这一点,但一种简单的方法是定义以类别为参数的子路线:
Router.route('/collections/:category',{
name: 'collections',
layoutTemplate: 'header',
data(){
return Collections.find({category: this.params.category});
}
});
然后,您的按钮代码就可以执行路由器.go('/collections/books'),但现在您可以有多个按钮,每个按钮都绑定到不同的类别。正如这里所建议的,您可以在模板中而不是在路由中管理订阅。因此,您可以:
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);
}
});
在模板中:
<button type="button" data-category="books" class="js-category">Books</button>
书籍
希望这将帮助您为您的找到正确的解决方案您的答案似乎很有希望,但对我来说不起作用。可能是因为对“collections”模板存在误解,所以我将名称改为“home”。
name
应该是要加载到路由上的模板的名称。如果您有一个layoutTemplate
,则该模板必须包含一个{{>yield}
以插入路由模板。