EmberJS registerHelper在Ember 1.8中动态渲染模板 [英] EmberJS registerHelper to dynamically render a template in Ember 1.8
问题描述
我想动态渲染一个模板,如下所示:
{{#layout layout in layouts}}
{{render layout.get('type')layout}}
{{/ each}}
问题是,render不会将变量作为其第一个参数,所以在较旧的EmberJS版本中,可以通过注册助手来实现解决方法:
Ember.Handlebars.registerBoundHelper('render_layout',
function(callContext,layout,options){
return Ember.Handlebars.helpers.render.call (callContext,
layout.get('type'),'layout',options);
});
在模板中:
{{#layout layout in layouts}}
/ pre>
{{render_layout this layout}}
{{/ each}}
不幸的是,这些东西在较新的ember版本中不起作用。
Ember.Handlebars.helpers.render.call
期望3个参数,但我无法让它们正确。有任何想法吗?
我试过:
(这个,layout.get('type'),选项)
在模板中:
{{#layout layout in layouts}}
{{render_layout布局}}
{{/ each}}
我得到
未捕获TypeError:无法读取属性'pushChildView'为null
...等等。
任何建议将不胜感激。
解决方案组件允许您指定
layoutName
。您可以根据传递到组件中的参数动态计算布局名称。App.XRenderComponent = Ember.Component.extend ({
tagName:,
layoutName:function(){
return this.get('displayLayout.type');
} .property('displayLayout'
});
然后你可以做
< script type =text / x-handlebarsid =index>
{{#each layout in model}}
{{x-render displayLayout = layout}}
{{/ each}}
< / script>
请参阅工作示例 here
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:
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. Any ideas? I've tried:(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
... and many others.
Any suggestions would be greatly appreciated.
解决方案Components allow you to specify
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
这篇关于EmberJS registerHelper在Ember 1.8中动态渲染模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!