EmberJs:在Handlebars-helper中呈现模板 [英] EmberJs: render template inside Handlebars-helper

查看:117
本文介绍了EmberJs:在Handlebars-helper中呈现模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况,我将模板名称存储在一个变量中。我注意到,如果你有例如

I have this situation in which I have the template-name stored in a variable. I noticed that if you have for example

var config = { view: "PeopleView", .... } ;

你不能这样做

{{view App[config.view]}}

这是可能的,我仍然对解决方案感兴趣!
无论如何,我决定用Handlebars helper来解决这个问题:

If this is possible, I'm still interested in the solution! Anyway, I decided to fix this with a Handlebars helper:

{{setVariableView config}}

...

Ember.Handlebars.registerBoundHelper('setVariableView', function(value, options) {
    return App[value.view].create(value) ; // <-- this doesn't work :)
}) ;

此时我不知道如何调用编译的PeopleView?
有什么建议吗?

At this point I don't know how to call the compiled PeopleView ? Any suggestions ?

干杯

Cheers

推荐答案

我们希望创建你的配置对象,就像你做的那样:

First you'll want to create your configuration object, as you've done:

App.config = Ember.Object.create({ view: 'App.MyView' });

然后你想注册你已经完成的助手,但需要修改它一点点,因为视图需要手动 append ed当你采取这种方法时:

And then you want to register your helper, which you've done, but it needs to be modified a little bit, since views need to be manually appended when you take this approach:

Ember.Handlebars.registerBoundHelper('setVariableView', function(value, options) {
    var view = eval(value.view).create();
    view.append();
});

现在您可以将配置文件传递给 setVariableView helper,它会呈现传递给你配置的视图:

And now you can pass in the configuration file to your setVariableView helper, and it will render the view that was passed in to your configuration:

{{setVariableView App.config}}

唯一值得关注的是 eval 。也许这是清晨没有帮助,但我现在想不出更好的选择。

The only concern is the utilisation of eval. Maybe it's the early morning that's not helping, but I can't think of a better alternative at the moment.

这篇关于EmberJs:在Handlebars-helper中呈现模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆