车把模板中的内联 javascript [英] inline javascript within handlebars template

查看:15
本文介绍了车把模板中的内联 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有办法在 Handlebars 模板中包含内联脚本吗?

Is there no way to have an inline script within a Handlebars template?

<script type="text/x-handlebars">
    I'm a row of type "foo"
    <script type="text/javascript">alert('hi');</script>
</script>

当上述模板呈现时,内联脚本被删除.

When the above template renders, the inline script is removed.

我想要做的是在页面上包含 disqus 小部件.小部件基本上是一些标记 + disqus 脚本.

What I am trying to do is include the disqus widget on a page. The widget is basically some markup + disqus script.

小部件将包含在我的应用程序的子视图中.默认情况下,子视图不可见,但会根据我的 Ember 应用程序代码中的某些逻辑显示.

The widget is to be included within a sub-view of my app. The subview is not visible by default but is displayed as per some logic in my Ember app code. ​

推荐答案

您必须将该小部件包装在自定义 Ember.View 中,以在包装视图的 didInsertElement 中处理实例化它并将其添加到 DOM.Ember 期望,尤其是在车把模板中,能够完全控制 DOM 操作,它结合了车把魔法和 Ember.View 创建来实现这一点.定义自定义视图后:

You'll have to wrap that widget in a custom Ember.View to handle instantiating it and adding it to the DOM, in the wrapper view's didInsertElement. Ember expects, especially inside handlebars templates, to have full control over DOM manipulation, and it does that with a combination of handlebars magic, and Ember.View creation. Once you've defined your customview:

MyApp.DisqusView = Em.View.extend({
    didInsertElement: function() {
        // create widget and append it to this.$()
    }
});

您可以在车把模板中使用它:

You can use it in your handlebars template:

{{view MyApp.DisqusView}}

出于安全原因,您的视图不应添加脚本标记,而应直接执行任何 JS 以插入小部件.

Your view should not add Script tags, for safety reasons, but should rather execute any JS directly to insert the widget.

这篇关于车把模板中的内联 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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