2016-03-20 4 views
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; 
    } 
}); 

答えて

2

使用{{#with }}:次に、あなただけの1つのヘルパーを必要とする

<template name="example"> 
{{#with doc}} 
    {{#if isEditorView}} 
    {{ > editor }} 
    {{else}} 
    {{ > user }} 
    {{/if}} 
{{/with}} 
</template> 

を:

Template.example.helpers({ 
    isEditorView: function() { 
    return Session.get('editorView') ? true : false; 
    }, 
    doc: function() { 
    return Collection.findOne(); 
    } 
}); 
関連する問題