phusick 提供的答案更好,但在这种情况下并不是一个真正的选择。我想出了这样的解决方案:
var dcHandles = [], dsHandles = [], dc = dojo.connect, ds = dojo.subscribe;
dojo.connect = function () {
var h = dc.apply ( dojo, arguments );
dcHandles.push ( h );
return h;
};
dojo.subscribe = function () {
var h = ds.apply ( dojo, arguments );
dsHandles.push ( h );
return h;
};
dojo.subscribe ( "unload", function () {
// restore dojo methods
dojo.connect = dc;
dojo.subscribe = ds;
var w, mll;
mll = dojo._windowUnloaders;
while (mll.length) {
( mll.pop () ) ();
}
if ( dijit.registry ) {
w = dijit.byId ( "topLevelItem1" );
w && w.destroyRecursive ();
w = dijit.byId ( "topLevelItem2" );
w && w.destroyRecursive ();
// destroy any other wijits
dijit.registry.forEach ( function ( w ) {
try
{
w.destroyRecursive ();
}
catch ( ex )
{
$.error ( ex );
}
} );
}
dojo.forEach ( dcHandles, function ( h ) {
dojo.disconnect ( h );
} );
dojo.forEach ( dsHandles, function ( h ) {
dojo.unsubscribe ( h );
} );
// reset monad-like values
my.global.values.value1 = null;
dcHandles = [];
dsHandles = [];
} );
上面给了我一些保证,在不更改大量代码的情况下,所有内容都会被取消注册/销毁/取消引用。