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 = [];
});
上面給了我一些保證,一切都被註銷/銷燬/解引用。