2

为了获得可理解的共享链接,我不想只._id在 url.name中放

Router.map(function () {
    this.route('here', {
        path: 'here/:_id/:name/',
        template: 'here'
    })
}) 

问题是该.name条目可以包含特殊字符,例如/.

www.example.com/here/1234/name_with special-characters like / (<-this slash) /

有没有办法替换 Iron-router 中的斜杠(和其他特殊字符)?
(如果有一种很好的方法来处理这个问题,也许在某些情况下我什至不再需要 id 了。)

如果我想使用<a href="{{pathFor 'showCourse'}}">
我不能使用通配符path: 'here/:_id/*

谢谢

4

2 回答 2

2

它不是 Iron Router 特有的,而是 JavaScript 的原生全局函数encodeURIComponent,并且decodeURIComponent仅用于此目的:

encodeURIComponent("foo/bar");   // returns "foo%2Fbar"
decodeURIComponent("foo%2Fbar"); // returns "foo/bar"

我在我的项目中所做的是添加一个名为的字段slug并编写一个函数,该函数从文档的标题生成一个 URL 友好的 slug检查集合以确保 slug 是唯一的(否则它会附加“-2”或“-3”等视情况而定)。使用slug每个文档唯一的或类似字段,您可以将其用作唯一的查询参数并放弃_id.

于 2013-11-17T21:10:44.210 回答
0

扩展 Geoffrey Booth 的答案,您可以使用模板助手来执行此操作。

定义一个模板助手来编码你的name值(我将它设为全局,以便所有模板都可以重用它):

Template.registerHelper('encodeName', function() {
  this.name = encodeURIComponent(this.name);
  return this;
});

然后,在您的模板中,您可以将此函数传递给 Iron-router 的pathFor助手:

<a href="{{pathFor 'showCourse' encodeName}}">

这适用于 Meteor 1.1.0.2。

于 2015-04-10T21:50:40.180 回答