Meteor iron router path or equivalent to assistant

I’m just trying to refer to the iron router route by name in the helper, instead of hard-coding the URL. Therefore, when the user clicks the button, they will be redirected to the Item routing of id with data-id attribute.

items.html

items.js

Template.items.events({< br />'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'
});

Of course I can use pathFor in templates like this:

View

Or specify data attributes:

But I want to know if there is an alternative/more concise way to do it at this point? If not, I may return to using tags.

You are lucky, this is actually very simple ( Although it is not clear in the document).

Given a route item with _id:

Router.route('/item/ :_id', {
name:'item'
});

There are actually two options. Option 1:

< pre>Router.go(‘item’, variableWithIdValue)

Or, if your object has a member variable/function that matches the route parameter (in this case, _id):

< p>

Router.go('item', itemInstance)

In the second case, Iron: Router noticed that the passed parameters match the parameters defined in the path path The member variable/function, so it calls it instead of using the passed parameter itself.

Edit: I should mention that you can also pass a hash, for example:

Router.go('item', {_id: variableWithIdValue})

This is actually described in the documentation here:

https://github.com/ EventedMind/iron-router/blob/devel/Guide.md#named-routes

I just tried to reference the iron router route by name in the helper instead of hard coding URL. So when the user clicks the button, they will be redirected to the item route with the id from the button’s data-id attribute.

items.html

items.js< /p>

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< /p>

Router.route('/item/:_id', {
name:'item'
});

I Of course you can use pathFor in such a template:

View

Or specify data attributes:

But wondering if there is an alternative/more concise way to do this? If not, I may return to using tags.

You are lucky, this is actually very simple (although it is not clear in the documentation).

< /p>

Given a route item with _id:

Router.route('/item/:_id', {
name: ' item'
});

There are actually two options. Option 1:

Router.go('item', variableWithIdValue) 

Or, if your object has member variables/functions that match the route parameters (in this case, _id):

Router.go ('item', itemInstance)

In the second case, Iron: Router notices that the passed parameter has a member variable/function that matches the parameter defined in the path path, so it calls it instead of Use the passed parameter itself.

Edit: I should mention that you can also pass a hash, for example:

Router.go('item', {_id: variableWithIdValue})

This is actually described in the documentation here:

https://github.com/EventedMind/iron-router/blob/devel/Guide. md#named-routes

Leave a Comment

Your email address will not be published.