Skip to content

Instantly share code, notes, and snippets.

@blacktambourine
Last active March 16, 2016 07:13
Show Gist options
  • Save blacktambourine/f9907a10472ee0218253 to your computer and use it in GitHub Desktop.
Save blacktambourine/f9907a10472ee0218253 to your computer and use it in GitHub Desktop.
Base Controller
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