PDA

View Full Version : Multiple events call the same function



drian
29 Mar 2011, 12:03 AM
Hi,


Can anyone guide me how i can assign the same function for multiple events of different components? I'm looking for a way to add all events at once and not assign them individually.


current method:

component1.on('event name1', myFunction);
component2.on('event name2', myFunction);
etc..

looking for something like writing a list of events and then assign them myFunction

thanks!

mcadirci
29 Mar 2011, 12:39 AM
var components = [
{
xtype: 'field',
id: 'someId',
name: 'fieldName',
...
events:[{
eventName: 'click',
handler: clickHandler
}
},
...
]after a declaration like this use for loop to add buttons and events
for( component in components){
var field = getComponent(component);
for( event in component.events )
field.on(event.eventName,event.handler);
This would be a nice coding style.

++ You need the handler functions with out parameters if you plan to use them on different components different event (not all events have same parameters)

tvanzoelen
29 Mar 2011, 12:40 AM
Do you mean something like this?

First create your function

//input vars depend on what the event returns
var myfunction = function(var1, var2, var2){

}

Secondly assign it to your components

mycomponent1.on('event', myfunction);
mycomponent2.on('event', myfunction);

There is also an Ext.Action object in Extjs if want to share functions in menu's or buttons.