LoadScript, LoadCSS, LoadJSON, LoadAsynchronously

Share your advanced knowledge/code with the community.
User avatar
eddy
Posts: 124
Joined: Thu Mar 27, 2014 8:34 am

LoadScript, LoadCSS, LoadJSON, LoadAsynchronously

Post by eddy »

- Loading file asynchronously with callback function
- Loading JS script
- Loading CSS stylesheet
- Loading JSON
based on : jquery AJAX and jquery XHR

Code: Select all

Procedure.i LoadAsynchronously(FileName.s, *OnLoadFunction, FileType.s)
  !return $.ajax({ url:v_FileName, dataType:v_FileType, beforeSend:function(jqxhr, settings) { jqxhr.url = settings.url; } })
  !.done(function(data, status, jqxhr) { p_OnLoadFunction(data,status,jqxhr.url); })
  !.fail(function(jqxhr, status, errorThrown) { p_OnLoadFunction('',status,jqxhr.url,jqxhr.status,errorThrown); });
EndProcedure 
  
Procedure.i LoadJSON(FileName.s, *OnLoadFunction)
  ProcedureReturn LoadAsynchronously(FileName, *OnLoadFunction, "json")
EndProcedure

Procedure.i LoadScript(FileName.s, *OnLoadFunction)
  ProcedureReturn LoadAsynchronously(FileName, *OnLoadFunction, "script")
EndProcedure

Procedure.i LoadCSS(FileName.s, *OnLoadFunction)
  Protected jqxhr=LoadAsynchronously(FileName, *OnLoadFunction, "text")
  !return v_jqxhr.done(function(data, status, jqxhr) { 
  !   $('<style></style>').appendTo('head').html(data);
  !})
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  
  ; *************************
  ; EXAMPLE 1 - advanced loading CSS
  ; *************************
  
  Procedure CSSLoaded(CSS.s, status.s, url.s, errorCode, error.s)
    If status="success"      
      Debug "=> Loading CSS: SUCCESSFUL"
      Debug "File: "+url
      ;Debug "File Content: "+CSS
    Else       
      Debug "=> Loading CSS: FAILED"
      Debug "File: "+url
      Debug "Error code: "+errorCode 
      Select errorCode 
        Case 401 : Debug "Error: Authentification Failed"
        Case 404 : Debug "Error: File Not Found"
        Case 500 : Debug "Error: Server Failed"
        Case 504 : Debug "Error: Server Timeout" 
        Default : Debug "Error: "+error
      EndSelect 
    EndIf
    Debug ""
  EndProcedure
  
  
  LoadCSS("http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css", @CSSLoaded())
  LoadCSS("http://netdna.bootstrapcdn.com/bootstrap/WRONG_URL/bootstrap.min.css", @CSSLoaded())
  ; // test new CSS
  !$('<button type="button"  class="btn btn-warning">Click Me!</button>').appendTo('body');
  !$('<button type="button"  class="btn btn-info">Click Me!</button>').appendTo('body'); 
  
  ; *************************
  ; EXAMPLE 2 - simple loading  script: just check if plugin is initialized
  ; *************************
  
  Procedure SimpleScriptLoaded()        
    ;
    ; HERE is your custom code to test if your plugin is fully functional (or to do something else)
    ; 
    Protected isPluginReady.b=0
    !v_isPluginReady = window.jsPDF ?1:0; 
    If isPluginReady
      Debug "=> Loading jsPDF Plugin is successful!"
    Else 
      Debug "=> Loading jsPDF Plugin has failed!"
    EndIf   
    Debug ""
  EndProcedure 
  
  LoadScript("https://raw.github.com/MrRio/jsPDF/master/dist/jspdf.debug.js",@SimpleScriptLoaded())
  
  
  ; *************************
  ; EXAMPLE 3 - advanced loading script
  ; *************************
  
  Procedure ScriptLoaded(script.s, status.s, url.s, errorCode, error.s)
    If status="success"
      ;
      ; HERE is your custom code to test if your plugin is fully functional (or to do something else)
      ;       
      Protected isPluginReady.b=0
      !v_isPluginReady = window.noty ?1:0; 
      If isPluginReady
        Debug "=> Loading Plugin: SUCCESSFUL"        
      Else 
        Debug "=> Loading Plugin: INITIALIZATION FAILED"
      EndIf            
      Debug "File: "+url
      ;Debug "File Content: "+script
    Else       
      Debug "=> Loading Plugin: FAILED"
      Debug "File: "+url
      Debug "Error code: "+errorCode 
      Select errorCode 
        Case 401 : Debug "Error: Authentification Failed"
        Case 404 : Debug "Error: File Not Found"
        Case 500 : Debug "Error: Server Failed"
        Case 504 : Debug "Error: Server Timeout" 
        Default : Debug "Error: "+error
      EndSelect 
    EndIf
    Debug ""
  EndProcedure 
  
  LoadScript("https://raw.githubusercontent.com/needim/noty/master/js/noty/packaged/jquery.noty.packaged.min.js",@ScriptLoaded())
  LoadScript("https://www.google.com/WRONG_URL/jquery.noty.packaged.min.js",@ScriptLoaded())
  
CompilerEndIf
Last edited by eddy on Thu May 01, 2014 10:31 am, edited 7 times in total.
User avatar
eddy
Posts: 124
Joined: Thu Mar 27, 2014 8:34 am

Re: LoadScript

Post by eddy »

Update
- Loading CSS
- updated "advanced loading" example
Post Reply