I'm just trying to refer to an iron router route by name in a helper, rather than hard coding the url. So when the user clicks on a button, they are redirected to the item route with the id from the data-id attribute of the button.
items.html
<button class="btn btn-default btn-xs pull-right" data-id={{_id}}>View</button>
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))
}
});
router.js
Router.route('/item/:_id', {
name: 'item'
});
I can of course use pathFor in a template like so:
<a href="{{pathFor 'item' id=_id}}">View</a>
Or specify a data attribute:
<button class="btn btn-default btn-xs pull-right" data-path={{pathFor 'item' id=_id}}>View</button>
But wondered if there is an alternative / neater way to do it? If not I might revert back to using tags.
UI._globalHelpers.pathFor(nameofRoute, param1, parmas2....)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With