Last active
March 16, 2016 07:13
-
-
Save blacktambourine/f9907a10472ee0218253 to your computer and use it in GitHub Desktop.
Base Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var baseModule = angular.module('corpApp.baseModule', ['ngRoute']); | |
baseModule.controller('BaseCtrl', ['$scope', '$q', '$timeout', function ($scope, $q, $timeout) | |
{ | |
//#region Initialise variables | |
$scope.pageErrors = {}; | |
$scope.data = {}; | |
//#endregion | |
//#region Web API functions | |
//Handle errors from Web API | |
$scope.HandleResponseErrors = function (response) | |
{ | |
if (response.HasResponseErrors) | |
{ | |
return $scope.onSubmitFail(); | |
} | |
} | |
//Call a web api service asynchronously | |
$scope.callWebApiService = function (service, serviceParameters) | |
{ | |
var deferred = $q.defer(); | |
var successCb = function (result) | |
{ | |
deferred.resolve(result); | |
}; | |
var errorCb = function () | |
{ | |
deferred.reject(); | |
}; | |
service.get(serviceParameters, successCb, errorCb); | |
return deferred.promise; | |
}; | |
//#endregion | |
//#region generic failure handling | |
$scope.onSubmitFail = function () | |
{ | |
$scope.pageErrors = { "submitFail": true }; | |
return false; | |
} | |
//Are there server errors on this page | |
$scope.hasPageErrors = false; | |
$scope.$watch('pageErrors', function () | |
{ | |
$scope.hasPageErrors = !jQuery.isEmptyObject($scope.pageErrors); | |
}, true); | |
//#endregion | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment