PDA

View Full Version : Element.removeListener never removing handlers!



Animal
2 Dec 2006, 11:39 AM
I think it's to do with passing scope || this into YAHOO's remove listener. If I declare a listener with no scope (such as one bound with yui-ext function extensions), and then remove it with no scope, it's never removed:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script src="js/yahoo.js" type="text/javascript"></script>
<script src="js/dom.js" type="text/javascript"></script>
<script src="js/event.js" type="text/javascript"></script>
<script src="js/animation.js" type="text/javascript"></script>
<script src="js/dragdrop.js" type="text/javascript"></script>
<script src="js/yui-ext.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css/yui-ext.css"></link>
<script type="text/javascript">
YAHOO.util.Event.addListener(window, "load", function()
{

var e = getEl("foo");
e.on("click", test);
e.removeListener("click", test);
});
function test()
{
alert("clicked");
}
</script>
</head>
<body>


<div id="foo" style="background-color:red;height:50px;width:100px"></div>
</body>
</html>

Animal
2 Dec 2006, 12:16 PM
This change to removeListener fixes it:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script src="js/yahoo.js" type="text/javascript"></script>
<script src="js/dom.js" type="text/javascript"></script>
<script src="js/event.js" type="text/javascript"></script>
<script src="js/animation.js" type="text/javascript"></script>
<script src="js/dragdrop.js" type="text/javascript"></script>
<script src="js/yui-ext.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css/yui-ext.css"></link>
<script type="text/javascript">
YAHOO.override(YAHOO.ext.Element, {
removeListener : function(eventName, handler, scope){
YAHOO.util.Event.removeListener(this.dom, eventName, handler);
return this;
}
});
YAHOO.util.Event.addListener(window, "load", function()
{
var e = getEl("foo");
e.on("mouseover", test); // Just to be sure we're still adding handlers.

var f1 = test.createCallback("window, true");
var f2 = test.createCallback("window");
var f3 = test.createCallback("none");
e.on("click", f1, window, true);
e.on("click", f2, window);
e.on("click", f3);
e.removeListener("click", f1, window, true);
e.removeListener("click", f2, window);
e.removeListener("click", f3);
});
function test(p)
{
alert("clicked " + p);
}
</script>
</head>
<body>


<div id="foo" style="background-color:red;height:50px;width:100px"></div>
</body>
</html>

jack.slocum
3 Dec 2006, 5:30 AM
Finally a bug related to the .12 yui release. I added that change and it works perfect, thanks!