Changeset 527


Ignore:
Timestamp:
07/19/06 00:29:31 (13 years ago)
Author:
mokhet
Message:

enhance loadback function.

  • callback function is now optional
  • application scope of the callback function can be forced
  • a Bonus object can be send to the callback function

for instance, when we were needed to do this :

var bonus = {foo:'bar'};
var editor = this;
HTMLArea._loadback('a_file.js', function() { editor.myCallback(bonus); });

we now do this instead :

var bonus = {foo:'bar'};
HTMLArea._loadback('a_file.js', this.myCallback, this, bonus );

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/htmlarea.js

    r526 r527  
    14201420  if ( typeof Dialog == 'undefined' ) 
    14211421  { 
    1422     // why can't we use the following line instead ? 
    1423 //    HTMLArea._loadback(_editor_url + 'dialog.js', this.generate ); 
    1424     HTMLArea._loadback(_editor_url + 'dialog.js', function() { editor.generate(); } ); 
     1422    HTMLArea._loadback(_editor_url + 'dialog.js', this.generate, this ); 
    14251423    return false; 
    14261424  } 
     
    14281426  if ( typeof HTMLArea.Dialog == 'undefined' ) 
    14291427  { 
    1430     // why can't we use the following line instead ? 
    1431 //    HTMLArea._loadback(_editor_url + 'inline-dialog.js', this.generate ); 
    1432     HTMLArea._loadback(_editor_url + 'inline-dialog.js', function() { editor.generate(); } ); 
     1428    HTMLArea._loadback(_editor_url + 'inline-dialog.js', this.generate, this ); 
    14331429    return false; 
    14341430  } 
     
    14361432  if ( typeof PopupWin == 'undefined' ) 
    14371433  { 
    1438     // why can't we use the following line instead ? 
    1439 //    HTMLArea._loadback(_editor_url + 'ipopupwin.js', this.generate ); 
    1440     HTMLArea._loadback(_editor_url + 'popupwin.js', function() { editor.generate(); } ); 
     1434    HTMLArea._loadback(_editor_url + 'popupwin.js', this.generate, this ); 
    14411435    return false; 
    14421436  } 
     
    24802474  var plugin_file = dir + "/" + plugin; 
    24812475 
    2482   if ( callback ) 
    2483   { 
    2484     HTMLArea._loadback(plugin_file, function() { callback(pluginName); }); 
    2485   } 
    2486   else 
    2487   { 
    2488     /** 
    2489     * @todo : try to avoid the use of document.write, it's evil 
    2490     * @todo : better yet, try to update HTMLArea._loadback to let it include 
    2491     *         the file without a callback function 
    2492     *  try 
    2493     *  { 
    2494     *    var head = document.getElementsByTagName('head')[0]; 
    2495     *    var script = document.createElement('script'); 
    2496     *    script.type = "text/javascript"; 
    2497     *    script.src = plugin_file; 
    2498     *    head.appendChild(script); 
    2499     *  } 
    2500     *  catch(ex) 
    2501     *  { 
    2502     *    document.write('<script type="text/javascript" src="' + plugin_file + '"></script>'); 
    2503     *  } 
    2504     */ 
    2505     document.write('<script type="text/javascript" src="' + plugin_file + '"></script>'); 
    2506   } 
     2476  HTMLArea._loadback(plugin_file, callback ? function() { callback(pluginName); } : null); 
    25072477  return false; 
    25082478}; 
     
    64716441}; 
    64726442 
    6473  
    6474 HTMLArea._loadback = function(src, callback) 
    6475 { 
    6476   var head = document.getElementsByTagName("head")[0]; 
    6477   var evt = HTMLArea.is_ie ? "onreadystatechange" : "onload"; 
    6478  
    6479   var script = document.createElement("script"); 
    6480   script.type = "text/javascript"; 
    6481   script.src = src; 
    6482   script[evt] = function() 
    6483   { 
    6484     if ( HTMLArea.is_ie && ! ( /loaded|complete/.test(window.event.srcElement.readyState) ) ) 
    6485     { 
    6486       return; 
    6487     } 
    6488     callback(); 
    6489   }; 
    6490   head.appendChild(script); 
     6443/** 
     6444 * Load a javascript file by inserting it in the HEAD tag and eventually call a function when loaded 
     6445 * @param {string} U (Url)      Source url of the file to load 
     6446 * @param {object} C {Callback} Callback function to launch once ready (optional) 
     6447 * @param {object} O (scOpe)    Application scope for the callback function (optional) 
     6448 * @param {object} B (Bonus}    Arbitrary object send as a param to the callback function (optional) 
     6449 * @public 
     6450 */ 
     6451HTMLArea._loadback = function(U, C, O, B) 
     6452{ 
     6453  var S = document.createElement("script"); 
     6454  S.type = "text/javascript"; 
     6455  S.src = U; 
     6456  if ( C ) 
     6457  { 
     6458    S[HTMLArea.is_ie ? "onreadystatechange" : "onload"] = function() 
     6459    { 
     6460      if ( HTMLArea.is_ie && ! ( /loaded|complete/.test(window.event.srcElement.readyState) ) ) 
     6461      { 
     6462        return; 
     6463      } 
     6464      C.call(O ? O : this, B); 
     6465    }; 
     6466  } 
     6467  document.getElementsByTagName("head")[0].appendChild(S); 
    64916468}; 
    64926469 
Note: See TracChangeset for help on using the changeset viewer.