extjs-如何为子组件的自定义事件创建侦听器 [英] extjs - How to create listener for child component's custom event
问题描述
我正在使用具有另一个子面板(例如childPanel)的面板(例如parentPanel).如何从childPanel触发将由parentPanel捕获的自定义事件?
I am using a panel (say parentPanel) having another child panel (say childPanel). How can i fire a custom event from childPanel that will be caught by parentPanel?
推荐答案
像这样:
Ext.define('Ext.ux.form.Panel', {
extend: 'Ext.form.Panel',
title: 'Simple Form',
bodyPadding: 5,
width: 350,
layout: 'anchor',
defaults: {
anchor: '100%'
},
defaultType: 'textfield',
initComponent: function() {
var me = this;
me.items = [{
fieldLabel: 'First Name',
name: 'first',
allowBlank: false
}];
me.callParent(arguments);
},
afterRender: function() {
var me = this;
me.callParent(arguments);
// in case of a form you can also use the findField() method
// I used down() because it will work with all sort of containers
me.down('textfield[name=first]').on('change',me.onFirstnameChange,me);
},
onFirstnameChange: function(field) {
// custom handler
}
});
仅在单个实例上执行此操作的方式与希望使用afterrender
事件而不是使用静态
Doing this just on single instances work the same way expect that you will need to use the afterrender
event instead of the static template method.
注意: 如果这不符合您的需求,则您需要发布更详细的问题(带有示例代码)以获得更详细的答案.
Note: I this doesn't fit your needs you will need to post a more detailed question (with example code) for a more detailed answer.
向新组件添加自定义事件:
Adding a custom event to a new component:
initComponent: function() {
// ...
me.addEvents('customeventname');
// ...
}
您现在可以在此组件的实例上注册此事件的侦听器,并通过调用fireEvent('customeventname', args)
触发事件,其中args
是您要用来调用每个侦听器的参数.
you can now register listeners to this events on instances of this component and fire the event by calling fireEvent('customeventname', args)
where args
are the arguments you want to call each listeners with.
这篇关于extjs-如何为子组件的自定义事件创建侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!