2

widgetを表示する Meteor アプリケーションがあるとします。ウィジェットは、次のいくつかの形式のいずれかを持つことができます。

<template name="textWidget">
    <h1>{{myTitle}}</h1>
    <p>{{myTextContent}}</p>
</template>

<template name="imgWidget">
    <h1>{{myTitle}}</h1>
    <img src="{{myImagePath}}" />
</template>

<template name="listWidget">
    <h1>{{myTitle}}</h1>
    <ul>
       {{#each items}}
          {{> listWidgetItem}}
       {{/each}}
    </ul>
</template>

特定のレコード タイプを指定して特定のテンプレートをレンダリングするための適切なパターンはありますか?

素朴に、私は次のようなことができます:

<template name="masterTemplate">
    {{#each widgets}}
        {{#if widgetType "text"}}
            {{> textWidget}}
        {{else}}
            {{#if widgetType "img"}}
               {{> imgWidget}}
            {{else}}
               {{if ... }} ... {{/if}}
            {{/if}}
        {{#/if}}

    {{/each}}
</template>

次のようなヘルパーを使用します。

Template.masterTemplate.widgetType = function(cmp) {
    return cmp === this.data.type;
};

ただし、これは非常に扱いにくく、柔軟性に欠ける方法のように思えます。このユース ケースの明らかなライブラリまたはデザイン パターンが不足していますか?

4

1 に答える 1

4

次のようなことを試すことができます:

UI.registerHelper('widget', function () {
  var templateName = this.data.type + 'Widget';
  return Template[templateName].extend({ data: this });
});

次のように使用できます。

{{> widget context}}

また

{{#with context}}
  {{> widget}}
{{/with}}
于 2014-05-20T21:30:26.700 に答える