Almog
Almog

Reputation: 2837

MeteorJS iron-router pathFor issue

For some reason the pathFor function is not working when I have vars in my path. I have it working for a different controller and route but not this one. I'm not sure why and trying to better understand how the pathFor function works.

My route

this.route('topPublicLinks', {
        path: '/:permalinkUser/:permalink/top/:linksLimit?',
        layoutTemplate: 'layoutPublic',
        controller: TopicPagePublicBestLinksController
});

The controller

TopicPagePublicBestLinksController = TopicPagePublicController.extend({
    sort: {votes: -1, submitted: -1, _id: -1},
    nextPath: function() {
        return Router.routes.topPublicLinks.path({linksLimit: this.limit() + this.increment})
    }
});

and the pathfor

<a href="{{pathFor 'topPublicLinks'}}" type="button" class="btn btn-white {{activeButtonNav 'top'}}">Top Links</a>

Upvotes: 0

Views: 150

Answers (2)

Almog
Almog

Reputation: 2837

In my controller in the data return I wasn't returning anything permalinkUser and permalink was null

path: '/:permalinkUser/:permalink/top/:linksLimit?',

Once I added the data function and returned permalinkUser and permalink it worked

data: function() {
        return {
            permalinkUser: Topics.findOne({$and:[{permalinkUser: this.params.permalinkUser},{permalink: this.params.permalink}]}).permalinkUser,
            permalink: Topics.findOne({$and:[{permalinkUser: this.params.permalinkUser},{permalink: this.params.permalink}]}).permalink,
        };
    }

Upvotes: 0

mark
mark

Reputation: 1725

Unless the pathFor helper exists within a data context that specifies what the path vars should be, you have to pass them into the helper as parameters. For example, you would have to say something like

{{pathFor 'topPublicLinks' permalinkUser=someUser permalink=somePermalink linksLimit=someLimit}}

You could alternatively pass in values directly

{{pathFor 'topPublicLinks' permalinkUser='userA' permalink='permalink' linksLimit=10}}

Upvotes: 1

Related Questions