使用选择下拉菜单更改模板和订阅 [英] Changing Templates and Subscriptions with Select dropdown
问题描述
示例:如果我选择葡萄,我想订阅 grapes
集合,取消订阅所有其他集合,然后渲染一个新模板.如何在选择新选项并相应行动时倾听?
Example: If I choose grapes, I would like to subscribe to the grapes
collection, unsubscribe to all other collections and then render a new template. How do I listen for when a new option has been selected and act accordingly?
<select>
<option value="choose fruit">
<option value="grapes">
<option value="strawberries">
</select>
谢谢!
推荐答案
假设您有一个带有 select
元素的模板,如下所示:
Let's say you had a template with a select
element like this:
<template name="hello">
<select class="fruit">
<option value="grapes">grapes</option>
<option value="strawberries">strawberries</option>
</select>
</template>
然后你可以添加一个 change
事件,你可以用它来确定路线:
Then you could add a change
event which you could use to determine the route:
Template.hello.events({
'change .fruit': function (event) {
var value = $(event.target).val();
var routeName = routeNameFromFruit(value);
Router.go(routeName);
}
});
我假设您使用的是铁路由器,因此 Router.go
可用.
I'm assuming you are using iron-router, so Router.go
is available.
如果您使用 IR,您可以使用 等待.如果没有,那么您可以设置会话变量并使用全局 autorun 来启动和停止订阅.
If you are using IR, you can manage your subscription changes with waitOn. If not then you can set a session variable and use a global autorun to start and stop your subscriptions.
这篇关于使用选择下拉菜单更改模板和订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!