【发布时间】:2014-04-08 23:34:13
【问题描述】:
我正在开发调查/向导类型的流星应用程序。 该界面将只有两个导航按钮,其标题/值将根据给定用户当前所处的任何步骤动态确定。 我的问题是:我应该在这两个按钮的定义中使用什么标记语法来指示 Iron Router 更改路由。换句话说,我应该把这个放在按钮定义的什么地方: this.redirect('/anotherpath') ?
【问题讨论】:
标签: meteor
我正在开发调查/向导类型的流星应用程序。 该界面将只有两个导航按钮,其标题/值将根据给定用户当前所处的任何步骤动态确定。 我的问题是:我应该在这两个按钮的定义中使用什么标记语法来指示 Iron Router 更改路由。换句话说,我应该把这个放在按钮定义的什么地方: this.redirect('/anotherpath') ?
【问题讨论】:
标签: meteor
如果您的模板中有一个按钮。如果您使用的是来自 f3rland 的 hrefs 检查答案。
html:
<button #id="mybutton>m<Button</button>
你可以用 javascript 捕捉 buttonpress 事件
js
Template.myTemplate.events({
'click #myButton': function(event, template) {
event.preventDefault();
Router.go('anotherpath');
}
});
路由器应该是这样的:
Router.map(function() {
this.route('anotherpath', {
path: '/',
layoutTemplate: 'myLayout',
controller: myController
});
});
【讨论】:
我建议你在你的模板中使用Iron-RouterpathFor helper
<a href="{{pathFor 'template-name'}}">{{buttonText}}</a>
这会将用户重定向到相应的页面/模板。
你也应该看看new blaze pattern to define custom block helper
编辑: 您还可以使用自定义帮助程序进行动态路由,如 here
所述UI.registerHelper("_foo", function fooHelper() {
var templateName = figureOutNameDynamically(this.context.criticalInfo);
return Template[templateName];
});
<template name="foo">
{{> _foo context=.. atts=this}}
</template>
【讨论】:
您可以通过使用将自动调用 this.render() 的操作选项来执行此操作/更改路线。 你可以通过调用动作函数自己做,但我想最好使用 onBeforeAction/after 钩子
【讨论】: