0
我正在使用具有另一個子面板(稱爲childPanel)的面板(稱爲parentPanel)。我怎麼能從childPanel觸發一個自定義事件,它將被parentPanel捕獲?extjs - 如何爲子組件的自定義事件創建偵聽器
我正在使用具有另一個子面板(稱爲childPanel)的面板(稱爲parentPanel)。我怎麼能從childPanel觸發一個自定義事件,它將被parentPanel捕獲?extjs - 如何爲子組件的自定義事件創建偵聽器
像這樣:
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
}
});
這樣做只是單個實例相同的方式工作預計將需要改用靜態template method的afterrender
事件。
注: 我不符合您的需求,您需要發佈更詳細的問題(附帶示例代碼)以獲得更詳細的答案。
編輯:
添加自定義事件到一個新的組件:通過調用fireEvent('customeventname', args)
其中args
是
initComponent: function() {
// ...
me.addEvents('customeventname');
// ...
}
您現在可以註冊這個組件的實例監聽這個事件和火災事件你想調用每個聽衆的參數。
- 這對我來說很好。只是沒有使用組件預定義事件(在您的示例中爲「更改」)。我想在文本字段中激發一個自定義事件。請更新與孩子fireEvent() – Vivek
@VVK的例子嗯,我不知道如果我得到你的權利...我添加了一些更多的信息到我的文章。如果這仍然不能幫助你,你需要提供更多信息。您是否想爲新組件啓動自定義事件?自己以另一個組件的名義發起一個事件 - 如果是這樣,這裏的觸發器是什麼? – sra
- 謝謝。順便說一句,我可以在面板中捕獲customeventname而不添加監聽器到文本字段(比如我有很多文本字段,我不想爲每個字段找到並添加監聽器)。 – Vivek