Dojo Garbage Collection / Resource Release Methods?

I recently created what I thought was a page-based application that is now being used as an inline control. "Control" must be unloaded / reloaded several times. This causes problems with subscriptions and controls that have not been destroyed. I developed the necessary disconnect logic, registry.destroy, but this is tedious. Are there any better methods to create a collection of controls that can destroy?

Here's an example code showing what can be done using basic logic: http://pastebin.com/bUUBUMP9

I ask if there is an infrastructure similar to AppDomain where you can create something created in this context. Like embedding a control in an IFRAME ... but not.

+5
source share
2 answers

I see two practices that will make your life easier:

  • Dijit widgets are extensible dijit/_WidgetBase, and therefore the widget provides methods (dis)connectand (un)subscribe. You should use them instead of general purpose aspect.connect()and topic.subscribe()when connecting widgets, because in this way the widget will automatically turn off and automatically unsubscribe upon destruction, so you do not need to.

  • dijit/layout, . dijit/layout/ContentPane , DOM, destroyRecursive() ContentPane, . ( , Java JPanel).

, destroyRecursive() , .

+4

, 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 = [];


} );

The above gave me some confidence that everything becomes unregistered / destroyed / de-link without changing a lot of code.

0
source

All Articles