PDA

View Full Version : bind a clas to a function



Stephan123
16 Aug 2011, 4:21 AM
Hello !

Can i bind a class to a function like JQuery ?


$(".mole").bind('click',function(e){
...
});


Your sincerly

Stephan

Uberdude
16 Aug 2011, 5:37 AM
As I don't know jQuery I'm going to make an educated guess what that code does, please correct me if I am wrong: it selects all elements with the css class 'mole' and then attaches the specified function as an event listener to all of those elements for the 'click' event.

Maybe you can do something similar in Ext with DomQuery. However, when I'm using Ext I'm rarely selecting and manipulating html elements (or indeed their Ext wrapper Ext.Element) directly. Instead I operate at the Ext.Component level, for which observeClass does a similar thing:


Ext.util.Observable.observeClass(Acme.widget);
Acme.widget.on('click', function() {
...
});

This adds a click event listener to all instances of Acme.widget.

Stephan123
16 Aug 2011, 6:56 AM
Hello !

Thanks for the quick answer.
In the moment i test Ext.DomQuery and the Ext.util.Observable.

Here is my first test.
But the test don't work.


var test = Array();
test = Ext.DomQuery.select('div[class=mole]');
Ext.util.Observable.observeClass(test);
test.on('click',function(){
alert('click');
})


Have you an idea ?

Greetings

Stephan

Uberdude
16 Aug 2011, 7:41 AM
Ext.util.Observable.observeClass(test);

You can't do this, observeClass should take a class (constructor function) as a parameter, but you are passing it an array! There are 2 separate approaches, if you want to use elements, use DomQuery to grab them, then iterate them and add your event listener. If you want to use components, use observeClass.

As an aside 'var test = Array();' is pointless (and I always use the array literal [] anyway), just do 'var test = Ext.DomQuery...'