jQuery is a nice JavaScript library, we all know it. But not many of us are familiar with ExtJS. As I have now been working with both, I needed some time to adapt to the change from jQuery to ExtJS and rediscover how things are done the easiest way with both libraries.
One of the things I really liked about jQuery was the “live” event.
The jQuery documentation explains it very straightforward:
[with a live event you] attach a handler to the event for all elements which match the current selector, now or in the future
I wanted to have something similar in ExtJS, and, no surprise, Ext has it, but it’s not very obvious for a newcomer.
In jQuery you can easily say:
$('p').live('click', function(){
// do something on paragraph click
})
As it turns out, the “live” event in ExtJS is even more powerful and customizable.
In ExtJS, the equivalent of the above is:
Ext.getBody().on('click', function(event, target){
// do something on paragraph click
}, null, {
delegate: 'p'
})
For more options and configuration parameters, see Ext.Element::addListener API (Ext.Element.on() is a shorthand for the Ext.Element.addListener() method)
As can easily be seen, you can add live events not only to the body element, but to any element.
Ext.get('header').on('click', function(){
//only handle anchors with the "menu" css class,
//which are in the element with id="header"
}, this /* the scope */, {
delegate: 'a.menu'
})
You can further distinguish between the target elements in the event callback
Ext.get('header').on('click', function(e,target){
//an anchor with class="menu" has been clicked in
//an element with id="header"
// Ext.Element.is(selector) tests the current
// item for the given selector
// target is of type HtmlElement, not Ext.Element,
// so wrap in Ext.get()
if (Ext.get(target).is('.item')){
// do smth special with anchors with "item" class
}
}, this /* the scope */, {
delegate: 'a.menu'
})
You can see a live demo here. Thanks!
Event delegation with Ext JS is incredibly useful for sure. Nice write-up.
Nice post. It’s worth noting that when building an Ext JS application you are more likely to deal with events on Components rather than events on page elements. This is because of the beautiful abstraction that Ext JS provides: for example, you work with an Ext.grid.GridPanel JavaScript object as opposed to the underlying collection of tables and divs.
@Jonathan. You are absolutely right Jonathan! I just wanted to provide an insight on how one can do this, if needed. I will come up in a future post with other tips on events, this time on component events and global application events. Thanks for the visit!
@Jonathan @radu I use XTemplates for generating custom views inside of components, this is where I most often use event delegation – it’s not strictly a ‘dealing with page elements’ thing. Very useful for custom components.
@Frederick could you please give more details on how you are using XTemplates for generating custom views? Thanks!
Nice article
Just to mention. jQuery has delegate too http://api.jquery.com/delegate/ with the same behaviour like in ExtJS but as you would assume a little different syntax