在Ember View中动态加载javascript

时间:2012-03-20 21:26:29

标签: ember.js

是否可以在Ember View Handlebars模板中运行Javascript?我想在将模板添加到DOM时运行Javascript。

window.App = Ember.Application.create()

App.TestView = Ember.View.create
    tagName: 'div'
    template: Ember.Handlebars.compile '<script>console.log("some javascript");</script><div>This is a view</div>'

App.TestView.append()

1 个答案:

答案 0 :(得分:2)

视图上的didInsertElement函数在添加到DOM时会被调用,请参阅documentation

然后,您可以通过this.$()访问添加的视图,请参阅http://jsfiddle.net/NuaA6/

<强>车把

<script type="text/x-handlebars" data-template-name="tmpl" >
    hello from template
</script>​

<强>的JavaScript

App = Ember.Application.create({});

Ember.View.create({
    templateName: 'tmpl',
    didInsertElement: function() {
        console.log('view has been added to dom', this.$());
    }
}).append();