如何使自定义事件通过视图层次结构发泡? [英] How can I make custom events that bubble through the view hierarchy?

查看:84
本文介绍了如何使自定义事件通过视图层次结构发泡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有按钮视图,这是我写的一组Ember.js表单助手的一部分。例如。 MyAddressFormView具有以下模板:

  {{#form}} 
{{textArea address}}
{{submitButton}}
{{cancelButton}}
{{/ form}}

要处理提交按钮,我让提交表单事件气泡,并在MyAddressFormView的提交方法中处理。



但是,如何对取消按钮执行相同操作?例如,有没有办法可以在子视图中触发自定义的取消表单事件,让它起泡,并在祖先视图中处理它?<​​/ p>

解决方案

我建议您触发一个jQuery特殊事件,并在Ember应用程序上注册一个映射。例如:

  Ember.Application.create({
customEvents:{
// key is jquery event,value是在视图中使用的名称
formcancel:'formCancel'
}
});

在您的cancelButton视图中:

 点击:function(evt){
Ember。$()。trigger('formcancel')
}
pre>

这将以与其他映射的Ember DOM事件相同的方式起泡。


I have button views that are part of a set of Ember.js form helpers I wrote. E.g. MyAddressFormView has the following template:

{{#form}}
  {{textArea address}}
  {{submitButton}}
  {{cancelButton}}
{{/form}}

To handle the "Submit" button, I let the "submit" form event bubble, and handle it in the submit method of MyAddressFormView.

But how do I do the same for the "Cancel" button? For instance, is there any way I can trigger a custom "cancel form" event in a child view, let it bubble, and handle it in an ancestor view?

解决方案

I would suggest triggering a jQuery special event and registering the event with a mapping on your Ember app. For example:

Ember.Application.create({
  customEvents: {
    // key is the jquery event, value is the name used in views
    formcancel: 'formCancel'
  }
});

And in your cancelButton view:

click: function(evt){
  Ember.$().trigger('formcancel')
}

This will bubble in the same way that other mapped Ember DOM events do.

这篇关于如何使自定义事件通过视图层次结构发泡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆