PDA

View Full Version : Ext equivalent to the jQuery method .live()



jnicora
29 Nov 2012, 4:32 PM
Does such a thing exist or will I need to roll my own?

http://api.jquery.com/live/

Description: Attach an event handler for all elements which match the current selector, now and in the future.

evant
29 Nov 2012, 5:16 PM
You achieve it by using the delegate option:

http://docs.sencha.com/ext-js/4-1/#!/api/Ext.dom.Element-method-addListener



Ext.getBody().on('click', function(){
console.log('You clicked foo');
}, null, {
delegate: '.foo'
});

jnicora
30 Nov 2012, 8:19 AM
Had not seen the delegate argument, that is perfect - thanks!

jnicora
30 Nov 2012, 8:31 AM
Actually, one more quick question - delegate refers to a DOM selector, is there an option or a way to have delegate use ComponentQuery instead?

It would be easy enough to bake that myself, just asking if it's already built in.

jnicora
30 Nov 2012, 1:40 PM
Is this alos a viable solution?



Ext.util.Observable.observe(Ext.util.Observable);

Ext.util.Observable.on('click', function(context, event, options) {
// intercept here
});

evant
30 Nov 2012, 1:59 PM
The equivalent to live would be the controller in the MVC structure.

Otherwise, you could instantiate the EventBus yourself.

jnicora
30 Nov 2012, 2:41 PM
Is it possible to have a controller intercept an event? Making assumptions here, but I'm guessing the controller uses event delegation to bind listeners, which means a buttons "handler" will fire first, then the event will bubble up to the body and then call the appropriate delegate, the problem is that I need to fire my override first, then possibly stop propagation.

So far the best way I have found to do this is to observe a class, like "Ext.form.Basic" or "Ext.util.Observable" and listen to their events, like "actioncomplete" or "click" - this lets me run an override first then either cancel propagation or not.

I certainly didn't explain myself properly in my original post and I'm not entirely sure I am now.