(function () { "use strict"; // "Private" members var dataPath = "data/"; var dataPathP = "data"; var dataPathG ="data" var clientDataPath = "clientData/"; var version = ""; var electionType; var trimJsonRoot = function (json) { return json.Root === undefined ? json : json.Root; } var buildFullFilePath = function (fileName, isVersioned, isClientData) { //getDataPath(); var dataPathToUse = isClientData ? clientDataPath : dataPath; return dataPathToUse + fileName + (isVersioned ? ("_" + version) : "") + ".json"; } var getDataPath = function () { switch (electionType) { case "G": dataPath = dataPathG; break; default: dataPath = dataPathP; break; } } angular.module("jsonDataFetcherModule", []) .factory("jsonDataFetcher", ["$http", "$q", "errorHandler", "primaryService", function ($http, $q, errorHandler, primaryService) { return { // Fetch a single json file. fetchSingleJson: function (fileName, isVersioned, isClientData, fetchSuccessfulCallback) { var fullFilePath = buildFullFilePath(fileName, isVersioned, isClientData); var fetchSuccessful = false; $http.get(fullFilePath) .success(function (json) { var returnJson = trimJsonRoot(json); fetchSuccessful = true; fetchSuccessfulCallback(returnJson); }) .error(function (data, status) { // Handle Json retrieval errors if (!fetchSuccessful) { var msgString; var isCorruptJson = data === undefined && status === undefined; if (isCorruptJson) msgString = "\"" + fileName + "\" data is corrupt."; else msgString = "Failed to retrieve \"" + fileName + "\" data due to " + status + " error."; errorHandler.handleError(msgString); } }); }, // Fetch multiple json files simultaneously. Will only execute provided callback once all files are retrieved. fetchMultipleJson: function (fileNames, areVersioned, areClientData, fetchSuccessfulCallback) { var fetchSuccessful = false; // Begin simutaneous retrieval var promiseArray = []; for (var i = 0; i < fileNames.length; i++) { var fullFilePath = buildFullFilePath(fileNames[i], areVersioned[i], areClientData[i]); promiseArray.push($http.get(fullFilePath)); } // Process all fetches as a single promise. $q.all(promiseArray) .then(function (jsonArray) { for (var j = 0; j < fileNames.length; j++) jsonArray[j] = (trimJsonRoot(jsonArray[j].data)); fetchSuccessful = true; fetchSuccessfulCallback(jsonArray); }) .catch(function (rejection) { // Handle Json retrieval errors if (!fetchSuccessful) { var msgString; var isCorruptJson = rejection.config === undefined && rejection.status === undefined; if (isCorruptJson) msgString = "\"" + fileNames + "\" data is corrupt."; else msgString = "Failed to retrieve \"" + fileNames + "\" data due to " + rejection.status + " error."; errorHandler.handleError(msgString); } }); }, // Tell jsonDataFetcher what version to use so it can automatically include it in JSON requests. registerVersion: function (curVersion) { version = curVersion; }, // Tell jsonDataFetcher what version to use so it can automatically include it in JSON requests. registerElectionType: function (_electionType) { electionType = _electionType; } }; }]); }());