How to load partials / views / templates dynamically in Ember.js

后端 未结 4 1659
耶瑟儿~
耶瑟儿~ 2021-02-04 14:14

So I have the following setup.

On the main page, a list of generators is being displayed based on a list coming from a model using fixture data.

Now when one of

4条回答
  •  醉酒成梦
    2021-02-04 15:04

    You can create a dynamic partial helper that uses the passed in name to render with the {{partial}} helper.

    Ember.Handlebars.helper('dynPartial', function(name, options) {
      return Ember.Handlebars.helpers.partial.apply(this, arguments);
    });
    

    Then use this dynamic partial, {{dynPartial}} instead.

    {{#each item in controller}}
      {{dynPartial item.templateName}}
    {{/each}}
    

    For a generator with templateName of generator-1. This would render with the partial _generator-1. Note that the name of the template's id/data-template-name must begin with an underscore.

提交回复
热议问题