Javascript/Ajax - 手动从 Sys.EventHandlerList() 中删除事件处理程序

发布于 2024-07-12 03:27:26 字数 1344 浏览 5 评论 0原文

我有两个脚本控件,一个控制另一个,并且我已经成功地能够使用以下方法在父级上处理来自子级的事件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没企图 2024-07-19 03:27:26

我不确定 DOM 元素上的“control”expando 属性是否是引用控制对象的正确方法。 它由框架管理,正如您所看到的,我认为在调用您的处理时它已经被处理掉了。

您是否尝试过使用 $find 而不是 $get 并以这种方式重新处理您的引用?:

initialize: function() 
{
    this._autoControl = $find(this._autoCompleteID);

    this._onAutoCompleteSelected = Function
      .createDelegate(this, this.handleAutoCompleteSelected);

    this._autoControl.addItemSelected(this._onAutoCompleteSelected);
}

dispose: function() 
{
    this._autoControl.removeItemSelected(this._onAutoCompleteSelected);
    this._autoControl = null;
}

哦,是的,您引用存储在 this._autoComplete< 中的 DOM 元素的位置/code> 您改为遍历控制对象本身:

this._autoControl.get_element();

因此基本上将“获取元素 => 获取控制对象”的逻辑反转为“获取控制对象 => 获取元素”。

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?:

initialize: function() 
{
    this._autoControl = $find(this._autoCompleteID);

    this._onAutoCompleteSelected = Function
      .createDelegate(this, this.handleAutoCompleteSelected);

    this._autoControl.addItemSelected(this._onAutoCompleteSelected);
}

dispose: function() 
{
    this._autoControl.removeItemSelected(this._onAutoCompleteSelected);
    this._autoControl = null;
}

Oh yeah and where you reference the DOM element stored in this._autoComplete you instead go through the control object itself:

this._autoControl.get_element();

So basically invert the logic of "get element => get control object" to "get control object => get element".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文