Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor iron router pathFor or equivalent in helper

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.

like image 498
gratz Avatar asked Jan 08 '23 23:01

gratz


1 Answers

UI._globalHelpers.pathFor(nameofRoute, param1, parmas2....)
like image 195
cesarve Avatar answered Jan 21 '23 12:01

cesarve