2012-03-20 55 views
2

是否可以在Ember View Handlebars模板中運行Javascript?我想在模板添加到DOM時運行Javascript。在Ember View中動態加載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() 

回答

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(); 
+0

你好,是可以下載的外部這樣的腳本?如果我有一個使用該腳本的頁面,我是否可以以某種方式加載它以便在此頁面上使用? – 2014-07-29 18:50:20

+0

**編輯**我結​​合這裏的信息在這裏:http://madhatted.com/2013/6/29/lazy-loading-with-ember做我想要的。謝謝! – 2014-07-29 19:01:37