繁体   English   中英

如何用流星雨渲染与路线相关的子标题?

[英]How render a route relevant subheader with meteor blaze?

我要查看的所有内容的最终结果是,副标题没有像我希望的那样呈现在屏幕上。

当前,有一个mongo collection子texth其中包含category字段和texth字段。

Subheader = new Mongo.Collection('subheader');

Meteor.methods({
  subheaderInsert: function(subheaderIdAttributes) {
  check(String);
  check(subheaderIdAttributes, {
    texth: String,
    category: String
  });
  var subheaderId = _.extend(postAttributes, {
    submitted: new Date()
  });
  var subheaderId = SubheaderId.insert(subheader);
    return {
    _id: subheaderId
  };
}
});

有一条预订子标题和其他页面数据的路由。

Router.route('/', {
  name: 'home',
    controller: MissionstatementpostController,
    waitOn:function () {
  return Meteor.subscribe('subheader', 'home');
  }
});

发布功能似乎可以正常工作。

Meteor.publish('subheader', function(cat) {
  return Subheader.find({category: cat});
});

mongodb集合中的正确文档正在到达客户端。 可以看到

Subheader.findOne(); output Object {_id: "NPo5cwqgjYY6i9rtx", texth: "ex text", category: "home"}

问题从这里开始

由控制器在这种情况下加载的模板MissionstatementpostControllerpostlist

<template name="postsList">
    <div class="posts page">
          {{> subheader}}
    <div class="wrapper">
      {{#each posts}}
        {{> postItem}}
      {{/each}}
    </div> 
    {{#if nextPath}}
      <a class="load-more" href="{{nextPath}}">Load more</a>
    {{else}}
      {{#unless ready}}
        {{> spinner}}
      {{/unless}}
    {{/if}}
  </div>
</template>

这是子标题模板

<template name="subheader">
    <div class="container">
        <p>{{{texth}}}</p>
    </div>
</template>

那我搞砸了什么?

谢谢

您必须为子标题模板创建模板助手。 仅返回texth字段,帮助程序将如下所示。

Template.subheader.helpers({
  texth: function() {
    var sh = Subheader.findOne();
    return sh && sh.texth;
  }
});

您可以返回整个文档,并在模板中使用#with帮助器。

Template.subheader.helpers({
  subh: function() {
    return Subheader.findOne();
  }
});

<template name="subheader">
    {{#with subh}}
    <div class="container">
        <p>{{{texth}}}</p>
    </div>
    {{/with}}
</template>

您可以在Meteor Docs上找到有关模板帮助器的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM