1

所以,我正在做一个 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 聚合的结果,如下所示:

(服务器/methods.js 摘录):

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

4

1 回答 1

1

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

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

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

{{pathFor "browse-class" class}}

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

path: "/browse/:_id"

然后它将使用_idfrom 类正确生成 URL。

对于链接的文本,我怀疑您是否要显示_id,您的课程文档可能包含“标签”,因此您可以使用它:

<a href="{{ pathFor 'browse-class' }}">{{ label }}</a>
于 2014-08-20T20:40:36.417 回答