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

Rajat
  • 32,970
  • 17
  • 67
  • 87

1 Answers1

10

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.

Christopher Swasey
  • 10,392
  • 1
  • 31
  • 25
  • +1, and AFAIK, the idea of handlebars is to have a logic-less templates – louiscoquio May 03 '12 at 13:36
  • @louiscoquio There is no logic in my handlebars template. The logic is in JS on when to show the template. The template just has a script tag within itself. – Rajat May 04 '12 at 03:42
  • @Rajat I've made a mistake so, my apologies. – louiscoquio May 04 '12 at 05:51
  • Handlebars templates do expect to be holistically aware of what is happening internally. That is to say, anything, logic or otherwise, that is happening in a handlebar template that isn't pure markup should be happening inside {{ helper }} notation. – Christopher Swasey May 05 '12 at 04:29
  • I'd appreciate if any of you help me with this very related question: http://stackoverflow.com/questions/12499921/access-attributes-of-the-dom-elements-from-ember-js-views – Aras Sep 20 '12 at 07:25