0
これは記事をユーザーとエディタビューとして表示する方法です。私はルータを使用しないので、私はonCreated
で購読する。流星:サブテンプレートに使用するデータを取得する
しかし、私はそれがタイトルとコンテンツのためにfindOne()をするのは好きではありません。私は1つの検索要求で完全なデータを取得し、両方のテンプレート(ユーザー/エディタ)でこのデータを使用できると思います。しかし、どうすればいいのですか?
テンプレート
<template name="example">
{{#if isEditorView}}
{{ > editor }}
{{else}}
{{ > user }}
{{/if}}
</template>
<template name="editor">
<input type="text" placeholder="title" value="{{title}}">
<textarea placeholder="article">{{content}}</textarea>
</template>
<template name="user">
<h1>{{title}}</h1>
{{{content}}}
</template>
データコンテキストを設定するヘルパー
Template.example.onCreated(function() {
Meteor.subscribe('articles');
});
Template.example.helpers({
isEditorView: function() {
return Session.get('editorView') ? true : false;
},
title: function() {
var doc = Collection.findOne();
return doc.title;
},
content: function(plain) {
var doc = Collection.findOne();
return doc.content;
}
});