我正在制作一个非常简单的 Ember 应用程序。为了我自己的理智,我更喜欢在 EmberViews 中通过 jQuery 执行必要的动画和简单的 dom 事件,例如鼠标悬停/点击。
假设我有这个:
App.SomeView = Ember.View.extend({
didInsertElement:function(){
this.$(element).hover(function(){
$(anotherElement).toggle();
});
}
});
我的模板是这样的:
<script type="text/javascript" data-template-name="routeName">
{{#view SomeView}}
<button {{action 'something' this}}> </button>
<element></element>
{{#if condition}}
<anotherElement></anotherElement>
{{/if}}
{{/view}}
</script>
我的 Controller 相应地支持像这样的“某事”操作:
App.SomeController = Ember.Controller.extend({
condition:false,
actions:{
something:function(){
this.set('condition',true);
}
}
});
一切都很好,但我希望“something”操作执行的是在我看来触发“didInsertElement”事件,以便“anotherElement” ' 可见性可以切换,而无需我重新写下该代码。
这有什么意义吗?我尝试使用'observables'但无法让它们工作。
或者,如果有更好的方法来做到这一点,我们也欢迎。
最佳答案
您可以在 View 中定义一个执行您想要的操作的操作,并将操作的目标指定为 View 。
<button {{action 'something' target="view"}}></button>
在你看来,
App.SomeView = Ember.View.extend({
...
actions: {
'something': function() {
// Do whatever you want.
}
}
});
关于jquery - 通过 EmberJS 操作触发 jQuery DOM 操作事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20255688/