Created
June 1, 2017 02:56
-
-
Save zhanser1k/297cb44b431316b065ec41c230c22257 to your computer and use it in GitHub Desktop.
promise for modal action
This file contains 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
(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