流星铁路由器路径或等效于助手

gra*_*atz 4 meteor iron-router

我只是试图在帮助器中按名称引用铁路由器路由,而不是硬编码URL.因此,当用户单击按钮时,它们将被重定向到具有来自按钮的data-id属性的id的项路由.

items.html

<button class="btn btn-default btn-xs pull-right" data-id={{_id}}>View</button>
Run Code Online (Sandbox Code Playgroud)

items.js

Template.items.events({
  'click button': function(e) {
    // This works but is hard coding the path
    Router.go("/item/" + ($(e.currentTarget).data('id')));
    // This doesn't work but is there an alternative method of doing this
    Router.go(pathFor('item' id=_id))
  }
});
Run Code Online (Sandbox Code Playgroud)

router.js

Router.route('/item/:_id', {
  name: 'item'
});
Run Code Online (Sandbox Code Playgroud)

我当然可以在这样的模板中使用pathFor:

<a href="{{pathFor 'item' id=_id}}">View</a>
Run Code Online (Sandbox Code Playgroud)

或者指定数据属性:

<button class="btn btn-default btn-xs pull-right" data-path={{pathFor 'item' id=_id}}>View</button>
Run Code Online (Sandbox Code Playgroud)

但是想知道是否有替代/更简洁的方法来做到这一点?如果不是,我可能会恢复使用标签.

小智 5

UI._globalHelpers.pathFor(nameofRoute, param1, parmas2....)
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你.我最终不得不使用`Router.path(routeName)`. (2认同)