内置JavaScript内部的句柄模板 [英] inline javascript within handlebars template

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

问题描述

 < script type =text / x -handlebars> 
我是一行类型foo
< script type =text / javascript> alert('hi');< / script>
< / script>

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



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



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

解决方案

您必须将自定义Ember.View中的该窗口小部件包含在其中来处理实例化和添加它到DOM,在包装器视图的didInsertElement中。 Ember希望,特别是在handlebars模板中,完全可以控制DOM操作,并且它可以通过组合魔术和Ember.View创建来实现。一旦您定义了自定义视图:

  MyApp.DisqusView = Em.View.extend({
didInsertElement:function (){
//创建小部件并将其附加到此$ $)
}
});

您可以在您的手柄模板中使用它:

  {{查看MyApp.DisqusView}} 

您的视图应该添加脚本标签,出于安全原因,但应该直接执行任何JS来插入小部件。


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.

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

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. ​

解决方案

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}}

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天全站免登陆