Javascript/Ajax - 手动从 Sys.EventHandlerList() 中删除事件处理程序
我有两个脚本控件,一个控制另一个,并且我已经成功地能够使用以下方法在父级上处理来自子级的事件:
initialize: function()
{
this._autoComplete = $get(this._autoCompleteID);
this._onAutoCompleteSelected = Function
.createDelegate(this, this.handleAutoCompleteSelected);
var autoControl = this._autoComplete.control;
autoControl.addItemSelected(this._onAutoCompleteSelected);
...
}
其中 addItemSelected(在子级上) 是:
addItemSelected: function(handler)
{
list = this.getEvents();
list.addHandler('userItemSelected', handler);
},
和 getEvents 是:
getEvents: function()
{
if (this._events == null)
{
this._events = new Sys.EventHandlerList();
}
return this._events;
},
问题是在处理父级时,我想做同样的事情:
dispose: function()
{
var autoControl = this._autoComplete.control;
autoControl.removeItemSelected(this._onAutoCompleteSelected);
...
}
但是,.control 不再存在。 我猜这是因为子控件已经被释放,因此 .control 属性不再起作用。
鉴于此,我决定运行子级上的事件列表并删除其中的所有事件处理程序。
dispose: function()
{
list = this.getEvents();
for(var item in list._list)
{
var handler;
handler = list.getHandler(item);
list.removeHandler(item, handler);
}
....
}
有一个更好的方法吗?
I have two script controls, one holds the other, and I have successfully been able to handle events from the child on the parent using:
initialize: function()
{
this._autoComplete = $get(this._autoCompleteID);
this._onAutoCompleteSelected = Function
.createDelegate(this, this.handleAutoCompleteSelected);
var autoControl = this._autoComplete.control;
autoControl.addItemSelected(this._onAutoCompleteSelected);
...
}
Where addItemSelected(on the child) is:
addItemSelected: function(handler)
{
list = this.getEvents();
list.addHandler('userItemSelected', handler);
},
and getEvents is:
getEvents: function()
{
if (this._events == null)
{
this._events = new Sys.EventHandlerList();
}
return this._events;
},
Problem is that on dispose of the parent, I want to do the same thing:
dispose: function()
{
var autoControl = this._autoComplete.control;
autoControl.removeItemSelected(this._onAutoCompleteSelected);
...
}
but, .control no longer exists. I'm guessing this is because the child control has already been disposed and thus the .control property no longer works.
In light of this, I decided to run though the event list on the child and remove all the event handlers in it.
dispose: function()
{
list = this.getEvents();
for(var item in list._list)
{
var handler;
handler = list.getHandler(item);
list.removeHandler(item, handler);
}
....
}
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定 DOM 元素上的“control”expando 属性是否是引用控制对象的正确方法。 它由框架管理,正如您所看到的,我认为在调用您的处理时它已经被处理掉了。
您是否尝试过使用
$find
而不是$get
并以这种方式重新处理您的引用?:哦,是的,您引用存储在
this._autoComplete< 中的 DOM 元素的位置/code> 您改为遍历控制对象本身:
因此基本上将“获取元素 => 获取控制对象”的逻辑反转为“获取控制对象 => 获取元素”。
I'm not sure that the "control" expando property on the DOM element is the right way to reference the control object. It is managed by the framework and as you're seeing, I think it's already munged by the time your dispose is called.
Have you tried using
$find
instead of$get
and reworking your references this way?:Oh yeah and where you reference the DOM element stored in
this._autoComplete
you instead go through the control object itself:So basically invert the logic of "get element => get control object" to "get control object => get element".