Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // =========================
  2. // Utilities
  3. // =========================
  4. var utils = utils || {};
  5.  
  6. // ========================================
  7. // Load template file into the DOM webpage
  8. // Params:
  9. //  url  - url to the template file (starting from the index.html directory)
  10. //  args - parameters to be put into the template, on the template mark
  11. //         them with double curly braces like: {{argumentName}}
  12. //  override - OPTIONAL override the previous template ( default: false )
  13. //  callback - OPTIONAL callback function run after template is loaded
  14. //
  15. // ========================================
  16. utils.loadTemplate = (function(f,u,n,c){
  17.     var $templateDom = $('#dynamic-template'),
  18.           $ajaxCall;
  19.  
  20.     if( typeof n === 'function' && typeof c === 'undefined' ) c = n;
  21.  
  22.     $ajaxCall = $.get(f).success(function(response){
  23.         var mustachedTemplate = Mustache.render(response,u);
  24.  
  25.         if( n === true ){
  26.             $templateDom.html(mustachedTemplate);
  27.         } else {
  28.             $templateDom.append(mustachedTemplate);
  29.         }
  30.  
  31.         if( typeof c === 'function' ){
  32.             c(mustachedTemplate);
  33.         }
  34.  
  35.     }).fail(function(response){
  36.         console.warn('Template error',response);
  37.        
  38.     });
  39.  
  40.     return true;
  41. });
  42.  
  43. // ========================================
  44. // Load a json file from the back-end ( via GET )
  45. // Params:
  46. //  url  - url/path to the external, back-end server
  47. //  callback - OPTIONAL callback function for the request
  48. //
  49. // ========================================
  50. utils.getData = utils.loadJSON = (function(f,u){
  51.     var $ajaxCall,
  52.             json;
  53.  
  54.     $ajaxCall = $.get(f).success(function(response){
  55.         if( typeof response === 'String'){
  56.             try {
  57.                 json = JSON.parse(response);
  58.             } catch(exception){
  59.                 console.warn('JSON error',exception);
  60.             }
  61.         } else {
  62.             json = response;
  63.         }
  64.  
  65.         if( typeof u === 'function' ){
  66.             u( json || null );
  67.         }
  68.     }).fail(function(response){
  69.         console.warn('JSON error',response);
  70.  
  71.     });
  72. });
  73.  
  74. // ========================================
  75. // Send data to the back-end ( via POST )
  76. // Params:
  77. //  url  - url/path to the external, back-end server
  78. //  data - parameters sent to the back-end
  79. //  callback - OPTIONAL function called after the back-end responds
  80. //
  81. // ========================================
  82. utils.sendData = utils.sendJSON = (function(f,u,n){
  83.     var $ajaxCall;
  84.  
  85.     $ajaxCall = $.post(f,u).success(function(response){
  86.         if( typeof response === 'String'){
  87.             try {
  88.                 json = JSON.parse(response);
  89.             } catch(exception){
  90.                 console.warn('JSON error',exception);
  91.             }
  92.         } else {
  93.             json = response;
  94.         }
  95.  
  96.         if( typeof u === 'function' ){
  97.             u( json || null );
  98.         }
  99.     }).fail(function(response){
  100.         console.warn('POST error',response);
  101.  
  102.     });
  103. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement