PDA

View Full Version : [OPEN] scope for 'this' is incorrect in createCallback



razorman8669
21 Jun 2007, 12:20 PM
The following code is supposed to add onClick event listeners to the list elements on a page. The event handler in this case just alerts the id of the list element that was clicked.


Ext.namespace('myNamespace.nav');

Ext.onReady(function() {
var NS = myNamespace.nav;
/* Get all list elements for the nav links.
*/
var menuItems = Ext.select("li[id$=_menuItem]", Ext.get("menu"));

/* Set up event handlers for each link for click and mouseover */
menuItems.each(function(el, myRef, index) {
this.addListener("click", NS.clickNavLink.createCallback(this));
});
});

myNamespace.nav = {

clickNavLink: function(menuItem) {

alert(moduleName.dom.id);

}
}
Now when i click on one of the list elements using the code above, it alerts with the ID of the last listElement on the page that was given an event listener (ie. if I had list elements as follows:


<ul id="menu">
<li id="one" />
<li id="two" />
<li id="three" />
<ul>
and i clicked on the list element with id="two" then the alert would say "three")

What SHOULD happen is the alert should say "two".

now if i change
this.addListener("click", NS.clickNavLink.createCallback(this)); into
this.addListener("click", NS.clickNavLink.createCallback(this.dom)); then it will work correctly.

--Why is it that when i pass 'this' (which is the current Ext element in the CompositeElement) as the argument for createCallback() it does not perform right, but when i pass 'this.dom' then it works?

Im assuming this is a bug, but i dont have any suggestions why it could cause this.

Wild Guess: maybe it has to do with the caching?

Environment:
Using Ext 1.0.1a
which is built onto YUI 2.2.2.
running in firefox 2.0.0.4

jack.slocum
21 Jun 2007, 1:29 PM
createCallback doesn't bind scope. To bind scope use createDelegate.