1

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

initialize: function() 
{
    this._autoComplete = $get(this._autoCompleteID);

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

    var autoControl = this._autoComplete.control;
    autoControl.addItemSelected(this._onAutoCompleteSelected);
    ...
}

其中 addItemSelected(on the child) 是:

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);
    }

    ....
}

有一个更好的方法吗?

4

1 回答 1

0

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".

于 2009-01-09T14:50:17.310 回答