简体   繁体   English

EmberJS registerHelper在Ember 1.8中动态呈现模板

[英]EmberJS registerHelper to dynamically render a template in Ember 1.8

I'd like to dynamically render a template, like this: 我想动态呈现一个模板,如下所示:

{{#each layout in layouts}}
   {{render layout.get('type') layout}}
{{/each}} 

The problem is that render doesn't expect a variable as its first arguments, so in the older EmberJS-versions, a workaround was possible by registering a helper: 问题在于,渲染器不希望将变量作为其第一个参数,因此在较旧的EmberJS版本中,可以通过注册帮助程序来解决:

Ember.Handlebars.registerBoundHelper('render_layout', 
  function(callingContext, layout, options) {
    return Ember.Handlebars.helpers.render.call(callingContext, 
                layout.get('type'), 'layout', options);
});

and in the template: 并在模板中:

{{#each layout in layouts}}
     {{render_layout this layout}}
{{/each}} 

Unfortunately the thing doesn't work in newer ember versions. 不幸的是,这在较新的余烬版本中不起作用。 Ember.Handlebars.helpers.render.call expects 3 arguments but I can not get them right. Ember.Handlebars.helpers.render.call需要3个参数,但我无法正确Ember.Handlebars.helpers.render.call Any ideas? 有任何想法吗? I've tried: (this, layout.get('type'), options) 我试过了:( (this, layout.get('type'), options)

and in the template: 并在模板中:

{{#each layout in layouts}}
     {{render_layout layout}}
{{/each}} 

I get Uncaught TypeError: Cannot read property 'pushChildView' of null 我收到Uncaught TypeError: Cannot read property 'pushChildView' of null

... and many others. ...还有许多其他

Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Components allow you to specify layoutName . 组件允许您指定layoutName And you can dynamically compute the layout name based on a parameter passed into the component. 然后,您可以根据传递给组件的参数动态计算布局名称。

App.XRenderComponent = Ember.Component.extend({
  tagName: "",
  layoutName: function(){
    return this.get('displayLayout.type'); 
  }.property('displayLayout'),
});

Then you can just do 那你就可以做

  <script type="text/x-handlebars" id="index">
    {{#each layout in model}}
      {{ x-render displayLayout=layout }}
    {{/each}}   
  </script>

See working example here 这里查看工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM