Changeset 13145

Show
Ignore:
Timestamp:
03/19/08 06:04:23 (10 months ago)
Author:
alex
Message:

updating code and docs to enable the addOnLoad(obj, function(){}); style which other dojo APIs handle. Some work to reduce code size as well. Fixes #5404

Location:
dojo/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • dojo/trunk/tests/_base/_loader/addLoadEvents.html

    r10248 r13145  
    88                <script type="text/javascript"> 
    99                        dojo.addOnLoad(function(){ 
    10                                 alert("addOnLoad works"); 
     10                                console.debug("addOnLoad(func) works"); 
    1111                        }); 
     12                        var scope = { 
     13                                named: function(){ console.debug("addOnLoad(obj, name) works"); }, 
     14                                refd: function(){ console.debug("addOnLoad(obj, func) works"); } 
     15                        }; 
     16                        dojo.addOnLoad(scope, "named"); 
     17                        dojo.addOnLoad(scope, scope.refd); 
     18                        // dojo.addOnLoad(scope, "named"); 
     19                        // dojo.addOnLoad(scope, scope.refd); 
     20 
    1221                        dojo.addOnUnload(function(){ 
    1322                                alert("addOnUnload works"); 
  • dojo/trunk/_base/_loader/loader.js

    r12942 r13145  
    146146                                mll[x](); 
    147147                        }catch(e){ 
     148                                throw e; 
    148149                                console.error("dojo.addOnLoad callback failed: " + e, e); /* let other load events fire, like the parser, but report the error */ 
    149150                        } 
     
    168169                while(mll.length){ 
    169170                        (mll.pop())(); 
     171                } 
     172        } 
     173 
     174        var onto = function(arr, obj, fn){ 
     175                if(!fn){ 
     176                        arr.push(obj); 
     177                }else if(fn){ 
     178                        var func = (typeof fn == "string") ? obj[fn] : fn; 
     179                        arr.push(function(){ func.call(obj); }); 
    170180                } 
    171181        } 
     
    182192                //      |       dojo.addOnLoad(functionPointer); 
    183193                //      |       dojo.addOnLoad(object, "functionName"); 
    184                 if(arguments.length == 1){ 
    185                         d._loaders.push(obj); 
    186                 }else if(arguments.length > 1){ 
    187                         d._loaders.push(function(){ 
    188                                 obj[functionName](); 
    189                         }); 
    190                 } 
     194                //      |       dojo.addOnLoad(object, function(){ /* ... */}); 
     195 
     196                onto(d._loaders, obj, functionName); 
    191197 
    192198                //Added for xdomain loading. dojo.addOnLoad is used to 
     
    200206 
    201207        dojo.addOnUnload = function(/*Object?*/obj, /*String|Function?*/functionName){ 
    202                 // summary: registers a function to be triggered when the page unloads 
     208                // summary: 
     209                //              registers a function to be triggered when the page unloads 
    203210                // example: 
    204211                //      |       dojo.addOnUnload(functionPointer) 
    205212                //      |       dojo.addOnUnload(object, "functionName") 
    206                 if(arguments.length == 1){ 
    207                         d._unloaders.push(obj); 
    208                 }else if(arguments.length > 1){ 
    209                         d._unloaders.push(function(){ 
    210                                 obj[functionName](); 
    211                         }); 
    212                 } 
     213                //      |       dojo.addOnUnload(object, function(){ /* ... */}); 
     214 
     215                onto(d._unloaders, obj, functionName); 
    213216        } 
    214217 
     
    223226 
    224227        dojo._callLoaded = function(){ 
    225                 //The "object" check is for IE, and the other opera check fixes an issue 
    226                 //in Opera where it could not find the body element in some widget test cases. 
    227                 //For 0.9, maybe route all browsers through the setTimeout (need protection 
    228                 //still for non-browser environments though). This might also help the issue with 
    229                 //FF 2.0 and freezing issues where we try to do sync xhr while background css images 
    230                 //are being loaded (trac #2572)? Consider for 0.9. 
     228 
     229                // The "object" check is for IE, and the other opera check fixes an 
     230                // issue in Opera where it could not find the body element in some 
     231                // widget test cases.  For 0.9, maybe route all browsers through the 
     232                // setTimeout (need protection still for non-browser environments 
     233                // though). This might also help the issue with FF 2.0 and freezing 
     234                // issues where we try to do sync xhr while background css images are 
     235                // being loaded (trac #2572)? Consider for 0.9. 
    231236                if(typeof setTimeout == "object" || (dojo.config.useXDomain && d.isOpera)){ 
    232237                        if(dojo.isAIR){