【问题标题】:Meteor helpers not working in each loop流星助手不在每个循环中工作
【发布时间】:2015-05-20 22:15:21
【问题描述】:

看起来相当基本的东西对我来说无法正常工作。我只是想在仪表板上显示公告列表,但我的助手似乎没有从出版物中提取数据。我的文件在下面。

publications.js

Meteor.publish('announcements', function() {
    return Announcements.find();
});

模板 JS(dashboard.js):

Template.sendersDashboard.helpers({
    announcements: function() {
        return Announcements.find({}, {sort: {createdAt: -1}});
    }
})

查看JS(dashboard.html):

<template name="dashboard_announcements">
    {{#each announcements}}
        {{> single_announcement}}
    {{else}}
        There are no Announcements to display.
        <br>
        <h5><a href="{{pathFor 'newAnnouncement'}}">Why don't you make one now?</a></h5>
    {{/each}}
</template>

当我在浏览器中查看页面时,我只看到{{else}}case。我检查了数据库,可以看到可用的记录。另外,我没有收到任何关于电话的错误。

非常感谢任何帮助、建议等。

【问题讨论】:

  • 您是否正确订阅?在控制台尝试 Announcements.find().fetch() 看看它是否返回空数组
  • 谢谢@Sindis。看起来我没有正确获取。通过下面的 lehtu,我错过了订阅。

标签: javascript meteor


【解决方案1】:

就像 Sindis 建议的那样,您可能在 dashboard.js 中缺少订阅。

Meteor.subscribe('announcements');

或者另一件事可能是您在错误的模板中使用了帮助程序。而不是:

Template.sendersDashboard.helpers({...

你应该有:

Template.dashboard_announcements.helpers({...

【讨论】:

  • 谢谢@lehtu。我错过了控制器中的订阅。您对模板的不同名称是正确的,但那是因为dashboard_announcements 嵌套在sendersDashboard 中。非常感谢。
  • 我以为你可以有这样的东西,但你永远不会在这里知道 :) 很高兴帮助你!
【解决方案2】:

您的 JS 和 HTML 中有不同的模板名称:

你在 JS 中的模板名称是sendersDashboard:

Template.sendersDashboard.helpers({
   //code
});

您的 HTML 模板名称是 dashboard_announcements:

<template name="dashboard_announcements">
  <!-- code -->
</template>

我建议你的模板使用驼峰命名,所以请修正 HTML 名称:

<template name="sendersDashboard">
  <!-- code -->
</template>

【讨论】:

  • 也谢谢。我也换成了camelCase。太多的红宝石世界仍然留在我的脑海中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 2015-11-13
  • 2016-07-31
  • 2018-01-25
相关资源
最近更新 更多