PDA

View Full Version : Custom events



sencha_san
4 Jan 2012, 8:53 PM
Hi all,
I'm new to sencha world :s,presently started working on Sencha Touch 2. Can someone explain how to register and fire custom events in ST2. I had hard time to run an example,googling the blogs etc.

Thanks in advance
Santosh

mitchellsimoens
5 Jan 2012, 7:50 AM
Ext.define('My.Container', {
extend : 'Ext.Container',
xtype : 'mycontainer',

initialize : function() {
this.callParent(arguments);

this.on('painted', 'checkSomething', this, { single : true });
},

checkSomething : function(cmp) {
//do some stuff here

this.fireEvent('customevent', this);
}
});

Ext.create('My.Container', {
fullscreen : true,
listeners : {
customevent : function(cmp) {
console.log(arguments);
}
}
});

The important part here is the line in red using fireEvent. To add more arguments to the event, you just add more to the fireEvent:


this.fireEvent('customevent', this, var1, var2);

sencha_san
5 Jan 2012, 8:43 PM
Thanks for your quick reply Mitchell, it worked perfectly. In the above example, "customevent" is fired when container is painted. Here what is "painted"?. how we are able to map "painted" to "customevent" ?

Thanks in advance
Santosh

mitchellsimoens
6 Jan 2012, 6:40 AM
painted in an event bubbled up from the element on the component and fired on the component when it is painted in the DOM. That's not what is important here, the important part is the fireEvent.

estesbubba
6 Jan 2012, 7:42 AM
As your application grows you'll probably want to fire events between controllers which is a little different. Here is how we are doing that:

http://www.sencha.com/forum/showthread.php?167234-Want-controllerB-to-listen-to-custom-event-fired-from-controllerA