由于某种原因,当我在路径中有变量时,pathFor函数不起作用。我有它为不同的控制器和路线工作但不是这个。我不确定为什么并试图更好地理解pathFor函数的工作原理。
我的路线
this.route('topPublicLinks', {
path: '/:permalinkUser/:permalink/top/:linksLimit?',
layoutTemplate: 'layoutPublic',
controller: TopicPagePublicBestLinksController
});
控制器
TopicPagePublicBestLinksController = TopicPagePublicController.extend({
sort: {votes: -1, submitted: -1, _id: -1},
nextPath: function() {
return Router.routes.topPublicLinks.path({linksLimit: this.limit() + this.increment})
}
});
和
的路径<a href="{{pathFor 'topPublicLinks'}}" type="button" class="btn btn-white {{activeButtonNav 'top'}}">Top Links</a>
答案 0 :(得分:1)
除非pathFor helper存在于指定路径变量应该是什么的数据上下文中,否则必须将它们作为参数传递给帮助程序。例如,您必须说出类似
的内容{{pathFor 'topPublicLinks' permalinkUser=someUser permalink=somePermalink linksLimit=someLimit}}
您也可以直接传递值
{{pathFor 'topPublicLinks' permalinkUser='userA' permalink='permalink' linksLimit=10}}
答案 1 :(得分:0)
在数据返回的我的控制器中,我没有返回任何permalinkUser和永久链接为null
path: '/:permalinkUser/:permalink/top/:linksLimit?',
一旦我添加了数据函数并返回了permalinkUser并且永久链接就可以了
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,
};
}