是否可以在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()
答案 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();