inline javascript within handlebars template

落花浮王杯 提交于 2019-11-30 23:13:14

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/10429164/inline-javascript-within-handlebars-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!