簡體   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