Skip to content

Instantly share code, notes, and snippets.

@zhanser1k
Created June 1, 2017 02:56
Show Gist options
  • Save zhanser1k/297cb44b431316b065ec41c230c22257 to your computer and use it in GitHub Desktop.
Save zhanser1k/297cb44b431316b065ec41c230c22257 to your computer and use it in GitHub Desktop.
promise for modal action
(function (angular) {
'use strict';
angular.module('app').factory('modalService', function ($uibModal) {
var opened = false;
var successCallback, errorCallback, finallyCallback;
var thenFunction = function (success, error) {
successCallback = success;
errorCallback = error;
return {
finally: finallyFunction
};
};
var finallyFunction = function (response) {
finallyCallback = response;
opened = false;
return {
then: thenFunction
};
};
var getSuccessCallback = function () {
if (angular.isFunction(successCallback)) {
successCallback();
}
};
var getErrorCallback = function () {
if (angular.isFunction(errorCallback)) {
errorCallback();
}
};
var getFinallyCallback = function () {
if (angular.isFunction(finallyCallback)) {
finallyCallback();
}
};
function open(params) {
if (!opened) {
opened = true;
$uibModal.open(params).result.then(getSuccessCallback, getErrorCallback).finally(function () {
opened = false;
getFinallyCallback();
});
}
return {
then: thenFunction,
finally: finallyFunction
};
}
return {
open: open
};
});
})(angular);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment