Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.factory('getApi', function($q, $log, $rootScope, loader, loaderScreen, $location) {
- var only_use_backup = 0;
- var only_backup_thresh = 2;
- return function(action, param, changedsettings) {
- var settings = {
- showerror: true,
- loadscreen: true,
- onlyjson: true,
- timeout: 10000,
- async: true,
- debug: false,
- type: 'GET'
- }
- angular.extend(settings, changedsettings);
- if (settings.loadscreen === true) {
- loaderScreen.push()
- }
- loader.push();
- //Creating a deferred object
- var deferred = $q.defer();
- param['action'] = action.toUpperCase();
- if (getToken() !== undefined) {
- param['token'] = getToken();
- }
- var c = config();
- //Calling Web API to fetch shopping cart items
- hashCode = function(s) {
- return s.split("").reduce(function(a, b) {
- a = ((a << 5) - a) + b.charCodeAt(0);
- return a & a;
- }, 0);
- };
- var use_backup = false;
- if (only_use_backup < only_backup_thresh) {
- var url = c['API'].replace("{action}", param['action']);
- } else {
- var url = c['API_backup'].replace("{action}", param['action']);
- use_backup = true;
- }
- var ajaxRequest = {
- type: settings.type,
- async: settings.async,
- url: url,
- data: param,
- timeout: settings.timeout,
- dataType: 'html'
- };
- ajaxRequest.success = function(data) {
- if (settings.onlyjson === true) {
- try {
- var json = angular.fromJson(data);
- if (settings.debug) {
- console.log("getAPI result", settings.type, action, "status: " + json.status, json);
- }
- if (json.status == 1) { //success
- deferred.resolve(json)
- if (settings.debug) {
- console.log("getAPI deferred", deferred);
- }
- updateIScroll();
- } else if (json.status == 3) { // auth error
- if (settings.debug) {
- console.log("getAPI auth error");
- }
- if (settings.showerror === true) {
- console.log("error at ", param);
- loginPopup("Login error", json.message);
- }
- deferred.reject("Login error", json.message);
- } else { //error
- if (settings.debug) {
- console.log("getAPI error", json.message);
- }
- if (settings.showerror === true) {
- printError("Status error", json.message);
- }
- if (json.redirect) {
- $location.path(json.redirect);
- }
- deferred.reject("Status error", json.message);
- }
- } catch (err) {
- if (settings.showerror === true) {
- printError("Server data is malformed", err + "<br/>Code:" + data);
- }
- deferred.reject("Server data is malformed", err + "<br/>Code:" + data);
- }
- } else {
- deferred.resolve(data);
- }
- $rootScope.loadScreen = false;
- loader.pop();
- if (settings.loadscreen === true) {
- loaderScreen.timeoutPop(100)
- }
- }
- ajaxRequest.error = function(json, a, b) {
- console.log("error", json, a, b);
- if (use_backup == false) {
- //rerun with backup URL
- var c = config();
- //if it exceeds the limit it will only use the backup url
- only_use_backup++;
- ajaxRequest.url = c['API_backup'].replace("{action}", param['action']);
- //run backup request
- $.ajax(ajaxRequest)
- console.log("retry");
- //prevent from recurring
- use_backup = true;
- } else {
- //Sending a friendly error message in case of failure
- deferred.reject("Server error", a);
- if (settings.showerror === true) {
- printError("An error occured while fetching items", a);
- }
- deferred.reject("An error occured while fetching items");
- loader.pop();
- if (settings.loadscreen === true) {
- loaderScreen.pop()
- }
- $rootScope.loadScreen = false;
- }
- }
- // run the request
- $.ajax(ajaxRequest);
- safeApply($rootScope);
- return deferred.promise;
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement