Advertisement
spidfire

Untitled

Oct 20th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.factory('getApi', function($q, $log, $rootScope, loader, loaderScreen, $location) {
  2.     var only_use_backup = 0;
  3.     var only_backup_thresh = 2;
  4.     return function(action, param, changedsettings) {
  5.         var settings = {
  6.             showerror: true,
  7.             loadscreen: true,
  8.             onlyjson: true,
  9.             timeout: 10000,
  10.             async: true,
  11.             debug: false,
  12.             type: 'GET'
  13.         }
  14.         angular.extend(settings, changedsettings);
  15.  
  16.  
  17.         if (settings.loadscreen === true) {
  18.             loaderScreen.push()
  19.         }
  20.         loader.push();
  21.         //Creating a deferred object
  22.         var deferred = $q.defer();
  23.         param['action'] = action.toUpperCase();
  24.         if (getToken() !== undefined) {
  25.             param['token'] = getToken();
  26.         }
  27.         var c = config();
  28.         //Calling Web API to fetch shopping cart items
  29.  
  30.         hashCode = function(s) {
  31.             return s.split("").reduce(function(a, b) {
  32.                 a = ((a << 5) - a) + b.charCodeAt(0);
  33.                 return a & a;
  34.             }, 0);
  35.         };
  36.  
  37.         var use_backup = false;
  38.         if (only_use_backup < only_backup_thresh) {
  39.             var url = c['API'].replace("{action}", param['action']);
  40.         } else {
  41.             var url = c['API_backup'].replace("{action}", param['action']);
  42.             use_backup = true;
  43.         }
  44.  
  45.         var ajaxRequest = {
  46.             type: settings.type,
  47.             async: settings.async,
  48.             url: url,
  49.             data: param,
  50.             timeout: settings.timeout,
  51.             dataType: 'html'
  52.         };
  53.         ajaxRequest.success = function(data) {
  54.  
  55.             if (settings.onlyjson === true) {
  56.                 try {
  57.                     var json = angular.fromJson(data);
  58.                     if (settings.debug) {
  59.                         console.log("getAPI result", settings.type, action, "status: " + json.status, json);
  60.                     }
  61.  
  62.                     if (json.status == 1) { //success
  63.                         deferred.resolve(json)
  64.                         if (settings.debug) {
  65.                             console.log("getAPI deferred", deferred);
  66.                         }
  67.                         updateIScroll();
  68.                     } else if (json.status == 3) { // auth error
  69.  
  70.                         if (settings.debug) {
  71.                             console.log("getAPI auth error");
  72.                         }
  73.                         if (settings.showerror === true) {
  74.                             console.log("error at ", param);
  75.                             loginPopup("Login error", json.message);
  76.                         }
  77.                         deferred.reject("Login error", json.message);
  78.                     } else { //error
  79.                         if (settings.debug) {
  80.                             console.log("getAPI error", json.message);
  81.                         }
  82.                         if (settings.showerror === true) {
  83.                             printError("Status error", json.message);
  84.                         }
  85.  
  86.                         if (json.redirect) {
  87.                             $location.path(json.redirect);
  88.                         }
  89.                         deferred.reject("Status error", json.message);
  90.                     }
  91.  
  92.                 } catch (err) {
  93.                     if (settings.showerror === true) {
  94.                         printError("Server data is malformed", err + "<br/>Code:" + data);
  95.                     }
  96.                     deferred.reject("Server data is malformed", err + "<br/>Code:" + data);
  97.                 }
  98.  
  99.             } else {
  100.                 deferred.resolve(data);
  101.             }
  102.             $rootScope.loadScreen = false;
  103.             loader.pop();
  104.             if (settings.loadscreen === true) {
  105.                 loaderScreen.timeoutPop(100)
  106.             }
  107.         }
  108.         ajaxRequest.error = function(json, a, b) {
  109.             console.log("error", json, a, b);
  110.             if (use_backup == false) {
  111.                 //rerun with backup URL
  112.                 var c = config();
  113.                 //if it exceeds the limit it will only use the backup url
  114.                 only_use_backup++;
  115.                 ajaxRequest.url = c['API_backup'].replace("{action}", param['action']);
  116.                 //run backup request
  117.                 $.ajax(ajaxRequest)
  118.                 console.log("retry");
  119.                 //prevent from recurring
  120.                 use_backup = true;
  121.             } else {
  122.                 //Sending a friendly error message in case of failure
  123.                 deferred.reject("Server error", a);
  124.                 if (settings.showerror === true) {
  125.                     printError("An error occured while fetching items", a);
  126.                 }
  127.                 deferred.reject("An error occured while fetching items");
  128.                 loader.pop();
  129.                 if (settings.loadscreen === true) {
  130.                     loaderScreen.pop()
  131.                 }
  132.                 $rootScope.loadScreen = false;
  133.             }
  134.         }
  135.         // run the request
  136.         $.ajax(ajaxRequest);
  137.  
  138.  
  139.         safeApply($rootScope);
  140.         return deferred.promise;
  141.  
  142.     };
  143. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement