在x-handlebars脚本中执行javascript [英] Executing javascript inside of x-handlebars script
问题描述
我正在使用Ember,并尝试在x-handlebars脚本段中执行一些JavaScript代码(不可能放在里面)?这是我的代码:
I am using Ember and am trying to execute some javascript code inside of a x-handlebars script segment (it isn't possible to put a inside a is it?). Here is my code :
<script type="text/x-handlebars" data-template-name="home">
<div id="slideshowfrontpage">
<script type="text/javascript">
$.vegas( 'slideshow' , {valign:'center',
backgrounds: homeImgURLs
})('overlay', {
src:'css/images/overlays/01.png'
});
</script>
</div>
</script>
我该怎么做这样的事情?谢谢!
How can I do something like this? Thank you!
推荐答案
当您每次将元素添加到DOM时都要启动javascript,您应该将该代码添加到 didInsertElement方法。
When you want to fire javascript off every time an element is added to the DOM you should add that code in a didInsertElement method.
Em.View.create({
templateName: 'home',
didInsertElement: function(){
this.$().vegas( 'slideshow' , {valign:'center',
backgrounds: homeImgURLs
})('overlay', {
src:'css/images/overlays/01.png'
});
}
});
这个$()实际上会让你看到该视图的DOM元素(这是非常好的)。每次视图被附加时,这将会被关闭一次,当它从DOM中删除时,有一个匹配的方法。
The this.$() will actually get you the DOM element of that view (which is really nice). Each time the view gets attached this will fire off once, there is a matching method for when it is removed from the DOM.
这篇关于在x-handlebars脚本中执行javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!