PathFor for an iron router meteor

I am rather confused about this issue. I have a template that has two paths like this:

Router.route('/companyDataManagement',{
        path:['/companyDataManagement','/companyDataManagement/:_id'],
        name: 'companyDataManagement',
        yieldTemplates:{
            'companyData':{to:'showCompanyData'},
            'companyDetails':{to:'showCompanyDetails'}
        }
});

      

This works great. But how to use pathFor

for this template. <a href="{{pathFor companyDataManagement}}">Click</a>

does not work

+3


source to share


1 answer


Can you confirm if the companyDataManagement in the link is the name passed from the helper, or if you assume it is the name of the named route? if it is last it must be encapsulated in single quotes as shown below

<a href="{{pathFor 'companyDataManagement'}}">Click</a>

      

If you want to then pass: _id to pathFor, this comes from the data context the link is in, if the data context does not provide an identifier, you need to declare the object to pass to the template inside the helper

Template.yourTemplate.helpers({
    myContextHelper: function(){
        return {_id:'XXXXXXXXX'}
    }
});

{{#with myContextHelper}}
    <a href="{{pathFor 'companyDataManagement'}}">Click</a>
{{/with}}

      

Which should give you / companyDataManagement / XXXXXXXXX

You can also pass query, hash, and data variables using, for example, query = "q = 1" or query = qstring, where qstring is an object from a helper or field in the myContextHelper object.



<a href="{{pathFor 'companyDataManagement' query=qstring }}">Click</a>

      

Also, and not necessarily to do this with the question, but hopefully helpful, it looks from your code as if you have: id as an extra part of the route in your path and that the templates themselves don't require: _id to be specified, and in this case can you just use? so that the part is optional:

path:'/companyDataManagement/:_id?',

      

You can also use this for your route prefix to eliminate the need to specify the path in the function:

Router.route('/companyDataManagement/:_id?',{

      

Hope this helps! Let me know if the above doesn't work to help troubleshoot if you can post a little more code surrounding it.

0


source







All Articles