【问题标题】:Meteor template not getting it's data for some reason. Confused about when to subscribeMeteor 模板由于某种原因没有得到它的数据。对何时订阅感到困惑
【发布时间】:2014-02-18 05:01:40
【问题描述】:

我的 Meteor 模板没有获取它的数据。它是一个路由的,它的所有车把字段都没有显示任何内容。

这是我的路由器的相关部分。

this.route('admin', {
    before: function() { if (!isAdmin()) Router.go('home'); },
    data: function()  { return Meteor.users.find(); }, 
    waitOn: function() { return Meteor.subscribe('adminAllUsers'); }
});

this.route('adminUser', {
    path: '/admin/:_id',
    before: function() { if (!isAdmin()) Router.go('home'); },
    // this is a potential problem area.
    data: function() { return Meteor.users.find(this.params._id); },
    waitOn: function() { return Meteor.subscribe('adminUser', this.params._id); }
});

我的出版物似乎在做他们的工作,因为当我在控制台中执行Meteor.users.find().fetch() 之类的操作时,带有正确字段的正确对象都会弹出。如果您想查看相关出版物,请询问。

我的模板:

<template name="admin">
    {{#each this}}
        // Here, since I'm using 'pathFor', it passes the id automatically. I'm not sure if that means I don't have to use the 'data' hook in the router.
        <a href="{{pathFor 'adminUser'}}">{{customer.firstname}} {{customer.lastname}}</a>: ${{customer.balance}}
    {{/each}}
</template>

<template name="adminUser">
    Admin user
    {{testing}}
    // None of these show up, not even the id.
    {{_id}}
    {{customer.firstname}}
    {{#each emails}}
        {{address}}
    {{/each}}
    {{#with customer}}
        {{firstname}} {{lastname}}
    {{/with}}
</template>

而且我的 javascript 中有这个助手:

Template.adminUser.testing = function() {
    console.log(this);
}

当这个助手记录它的日志时,它会打印:

Object {collection: LocalCollection, selector_id: "veW6jvfSM5Jc46Wbj", selector_f: function, sort_f: undefined, skip: undefined…}

当我深入研究它那里有一切,selector_id 是正确的路线。

为什么什么都没有显示?

【问题讨论】:

    标签: meteor handlebars.js


    【解决方案1】:

    这个很简单,一个RouteController的data方法会作为渲染模板的当前数据上下文。

    您的管理路线没问题,因为您从 Collection.find 返回了一个 LocalCursor 并使用 {{#each this}} 遍历光标。

    但是在 adminUser 中,您还返回了一个 LocalCursor,但您真正需要的是一个代表用户的普通对象,因为模板引用了它的成员。

    只需将 Collection.find(id) 更改为 Collection.findOne(id) 即可!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 2022-07-02
      • 2018-09-09
      • 2012-08-13
      • 1970-01-01
      相关资源
      最近更新 更多