【问题标题】:Meteor content not displaying流星内容不显示
【发布时间】:2015-06-10 19:18:28
【问题描述】:

我在使用 Flow Router 或我的模板级订阅时遇到问题,但数据没有在页面上呈现。

如果问题不是我在此处粘贴的问题,我已包含指向整个 github 存储库的链接:https://github.com/adjohnston/checkr-meteor

lib/routes.js

listRoutes.route('/:slug', {
  name: 'list',

  subscriptions: function (params) {
    this.register('singleList', Meteor.subscribe('singleList', params.slug));
  },

  action: function () {
    FlowLayout.render('mainLayout', {
      main: 'list'
    });
  }
});

服务器/publication/lists.js

Meteor.publish('singleList', function (slug) {
  return Lists.find({slug: slug});
});

client/lists/list.js

Template.list.helpers({
  singleList: function () {
    return Lists.find();
  }
});

client/lists/list.html

<template name="list">

  {{#if isSubReady}}
    {{#with singleList}}

      <h2>Name: {{name}}</h2>

    {{/with}}
  {{/if}}

</template>

解决方案

Change 将 Lists.find() 返回到 Lists.findOne(),因为发布 'singleList' 只返回一个结果。

client/lists/list.js

Template.list.helpers({
  singleList: function () {
    return Lists.findOne();
  }
});

【问题讨论】:

    标签: javascript meteor meteor-blaze


    【解决方案1】:

    尝试将您的 singleList 助手更改为 findOne

    Template.list.helpers({
      singleList: function () {
        var slug = FlowRouter.getParam("slug");
        return Lists.findOne({slug: slug});
      }
    });
    

    现在您正试图显示光标的name 属性,这是find() 返回的内容。您的车把中也不需要{{#with singleList}}

    【讨论】:

    • 所以问题是由于在我的帮助程序中使用 find() 而不是简单地使用 findOne() 引起的。我一直坚持使用#with,所以我不需要在所有属性前面加上 singleList。在帮助程序中使用参数是否比在发布中使用参数有好处?
    • 它们有不同的用途。您可以在发布中使用它来控制客户端需要哪些数据。您可能有理由将多个列表发布到路线,但在此特定帮助程序中一次只显示一个。这将取决于您的应用程序需求。
    猜你喜欢
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多