Changeset 1414


Ignore:
Timestamp:
02/22/18 08:51:56 (4 months ago)
Author:
gogo
Message:

Add an abort() method for the assetLoader, which is now used by loadLibrary for jQuery, so that if jQuery doesn't load properly, anything chained on that is aborted also, saves having errors in the console we can avoid when things looking to insert themselves into jQuery can't do so.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/XinhaCore.js

    r1413 r1414  
    42914291Xinha.includeAssets = function() 
    42924292{ 
    4293   var assetLoader = { pendingAssets: [ ], loaderRunning: false, loadedAssets: [ ] }; 
     4293  var assetLoader = { pendingAssets: [ ], loaderRunning: false, loaderPaused: false, loadedAssets: [ ] }; 
    42944294   
    42954295  assetLoader.callbacks = [ ]; 
     
    43254325    if(this.pendingAssets.length) 
    43264326    { 
     4327      if(this.loaderPaused) 
     4328      { 
     4329        this.loaderRunning = false; 
     4330        return; 
     4331      } 
    43274332      var nxt = this.pendingAssets[0]; 
    43284333      this.pendingAssets.splice(0,1); // Remove 1 element 
    43294334      switch(nxt.type) 
    43304335      { 
     4336        case 'command/abort': 
     4337          if((typeof nxt.condition != 'function' && nxt.condition) || (typeof nxt.condition == 'function' && nxt.condition())) 
     4338          { 
     4339            // Abort is in the context of the chained loader, we might have more things added 
     4340            //  in our own list and those should continue to be executed 
     4341            nxt.chainLoader.abort(); 
     4342          } 
     4343          return this.loadNext(); 
     4344 
    43314345        case 'text/css': 
    43324346          this.loadedAssets.push(nxt); 
     
    43374351          this.loadedAssets.push(nxt); 
    43384352          Xinha.loadScript(nxt.url, nxt.plugin, function() { self.loadNext(); }); 
     4353          return this; 
    43394354      } 
    43404355    } 
     
    43434358      this.loaderRunning = false; 
    43444359      this.runCallback();       
     4360      return this; 
    43454361    } 
    43464362  }; 
     
    44344450    return this; 
    44354451  }; 
     4452     
     4453  assetLoader.abortIf = function(condition) 
     4454  { 
     4455    // We use a dummy asset to insert, and return a chainable loader which will be started  
     4456    //  only if the condition is met (evaluated after previous assets have been loaded) 
     4457    //  
     4458    // Note that this means you can do 
     4459    //   
     4460    //  myAssetLoader.loadScript('blah.js') 
     4461    //               .abortIf(function(){ return true ; } ) 
     4462    //               .loadStyle('blah.css'); 
     4463    // 
     4464    //  myAssetLoader.loadScript('foo.js'); 
     4465    //   
     4466    // and even though the first chain was aborted after loading blah.js and blah.css  
     4467    //   never gets loaded, foo.js still does get loaded because it's in a different  
     4468    //   chain of assets. 
     4469    // 
     4470    // For a  practical example, see loadLibrary where jQuery is tested after it loads 
     4471    //  and if it doesn't then the chained (assuming jQuery) scripts are not loaded. 
     4472    // 
     4473    var waitAsset = { type: 'command/abort', condition: condition, url: null, plugin: null, chainLoader: Xinha.includeAssets() }; 
     4474    this.pendingAssets.push( waitAsset ); 
     4475    waitAsset.chainLoader.pause(); 
     4476    return waitAsset.chainLoader; 
     4477  }; 
     4478   
     4479  assetLoader.pause = function()  
     4480  { 
     4481    this.loaderPaused = true; 
     4482  }; 
     4483   
     4484  assetLoader.resume = function()  
     4485  { 
     4486    this.loaderPaused = false; 
     4487    if(!this.loaderRunning) this.loadNext(); 
     4488  }; 
     4489   
     4490  assetLoader.abort = function() 
     4491  { 
     4492    this.pendingAssets = [ ]; 
     4493    this.callbacks     = [ ]; 
     4494    this.loaderPaused = false; 
     4495    return this; 
     4496  }; 
    44364497   
    44374498  for(var i = 0 ; i < arguments.length; i++) 
     
    44834544      if(typeof jQuery == 'undefined') 
    44844545      { 
    4485         Xinha._libraryAssetLoader.loadScriptOnce('libraries/jquery-3.3.1.js'); 
     4546        // jQuery can be problematic if it (jQuery itself) fails to load 
     4547        //  due to old browsers, so we will return a chained loader which 
     4548        //  aborts anything chained on it if jQuery doesn't come into  
     4549        //  existence properly. 
     4550        return Xinha._libraryAssetLoader.loadScriptOnce('libraries/jquery-3.3.1.js') 
     4551                                        .abortIf(function(){ return typeof jQuery == 'undefined'; }); 
    44864552      } 
    44874553      break; 
Note: See TracChangeset for help on using the changeset viewer.