I have encountered a situation where I am working with two script controls, one containing the other. Successfully, I have managed to handle events from the child control on the parent using the following code snippet:
initialize: function()
{
this._autoComplete = $get(this._autoCompleteID);
this._onAutoCompleteSelected = Function
.createDelegate(this, this.handleAutoCompleteSelected);
var autoControl = this._autoComplete.control;
autoControl.addItemSelected(this._onAutoCompleteSelected);
...
}
The addItemSelected method on the child control is implemented as follows:
addItemSelected: function(handler)
{
list = this.getEvents();
list.addHandler('userItemSelected', handler);
},
The getEvents method is responsible for initializing the event list as shown below:
getEvents: function()
{
if (this._events == null)
{
this._events = new Sys.EventHandlerList();
}
return this._events;
},
The issue arises when trying to dispose of the parent control. The attempt is to mimic the initial process by running through the event handlers in the child control and removing them:
dispose: function()
{
var autoControl = this._autoComplete.control;
autoControl.removeItemSelected(this._onAutoCompleteSelected);
...
}
However, it appears that the ".control" property no longer exists at disposal, possibly due to the fact that the child control has already been disposed. This led me to think about iterating through the event list on the child control and clearing all event handlers within it:
dispose: function()
{
list = this.getEvents();
for(var item in list._list)
{
var handler;
handler = list.getHandler(item);
list.removeHandler(item, handler);
}
....
}
Given this scenario, I am open to suggestions on a more optimal approach to handling this situation. Is there a better way to achieve the desired result?