【问题标题】:How render a route relevant subheader with meteor blaze?如何用流星火焰渲染路线相关的子标题?
【发布时间】:2015-07-08 10:42:52
【问题描述】:

我将要讨论的所有内容的最终结果是子标题没有像我想要的那样在屏幕上呈现。

目前有一个带有 category 字段和 texth 字段的 mongo 集合子标题。

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>

那我搞砸了什么?

谢谢

【问题讨论】:

    标签: javascript mongodb meteor routes meteor-blaze


    【解决方案1】:

    您必须为您的子标题模板创建一个模板助手。要只返回 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 上找到有关模板助手的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 2016-05-18
      • 2016-06-10
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多