为什么不为此路由生成URL?

时间:2014-08-20 20:33:23

标签: meteor iron-router spacebars

所以,我正在研究一个Meteor项目,我无法正确地生成这条路线,或者根本就没有。

<template name="browseAll">
    <h3>List of classes with books available!</h3>
    <ul>
        {{#each aggCount}}
            <li><a href="{{ pathFor 'browse-class' }}">{{ _id }}</a> ({{ count }})</li>
        {{/each}}
    </ul>
</template>

正在迭代的数据是使用MongoInternals进行聚合的结果,如下所示:

(server / methods.js excerpt):

classCount: function() {

    // Attempt aggregation of the books table to count by class, maybe.
    var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
    var col = db.collection("books");
    var aggregateSync = Meteor._wrapAsync(col.aggregate.bind(col));
    var pipeline = [
        {$group: {_id: "$class", count: {$sum: 1}}},
        {$sort: {_id: 1}}
    ];
    var theAnswer = aggregateSync(pipeline);
    return theAnswer;

}

似乎数据正常通过,聚合中的样本数据(进入模板)如下所示:

[ { _id: 'ADNR1234', count: 2 }, { _id: 'ARTH1234', count: 1 } ]

我已经获得的模板代码,这是它应该使用的路线:

this.route('browse-class', {
    path: '/browse/:_class',
    data: function() {
        var booksCursor = Books.find({"class": this.params._class},{sort:{"createdAt": 1}});
        return {
            theClass: this.params._class,
            numBooks: booksCursor.count(),
            books: booksCursor
        };
    }
});

我不明白。数据正在显示,我想要做的是为browse-class(路由)生成一个URL,该URL将助手中的{{ _id }}值作为参数,以生成如下内容:

application.org/browse/CLSS

1 个答案:

答案 0 :(得分:1)

请注意,必须在正确设置数据上下文的情况下调用{{pathFor}}

{{#with class}}
  {{pathFor "browse-class"}}
{{/with}}

Optionnaly可以将数据上下文作为参数传递:

{{pathFor "browse-class" class}}

如果您定义了这样的路径路径,则在生成路径路径时使用提供给pathFor的数据上下文:

path: "/browse/:_id"

然后它将使用类中的_id来正确生成URL。

对于链接的文字,我怀疑你想要显示_id,你的班级文件可能包含一个&#34;标签&#34;所以你可以用这个:

<a href="{{ pathFor 'browse-class' }}">{{ label }}</a>
相关问题