【问题标题】:meteor app: how to properly use a button to influence the route流星应用程序:如何正确使用按钮来影响路线
【发布时间】:2014-04-08 23:34:13
【问题描述】:

我正在开发调查/向导类型的流星应用程序。 该界面将只有两个导航按钮,其标题/值将根据给定用户当前所处的任何步骤动态确定。 我的问题是:我应该在这两个按钮的定义中使用什么标记语法来指示 Iron Router 更改路由。换句话说,我应该把这个放在按钮定义的什么地方: this.redirect('/anotherpath') ?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    如果您的模板中有一个按钮。如果您使用的是来自 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
      });
    });
    

    【讨论】:

    • 这很好用!代替“另一条路径”,我调用了一个函数来确定NextStep(),它现在将根据持久 NavigationMap 模型对象的值动态计算下一步,并将返回适当的路径字符串,它将从“NextStep”模型字段
    【解决方案2】:

    我建议你在你的模板中使用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>
    

    【讨论】:

      【解决方案3】:

      您可以通过使用将自动调用 this.render() 的操作选项来执行此操作/更改路线。 你可以通过调用动作函数自己做,但我想最好使用 onBeforeAction/after 钩子

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 2015-05-27
        • 1970-01-01
        • 1970-01-01
        • 2017-11-25
        相关资源
        最近更新 更多