Created
April 24, 2013 11:39
-
-
Save endeepak/5451506 to your computer and use it in GitHub Desktop.
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
| angular.module('overlay', []) | |
| .factory('overlay', function () { | |
| var enabled = true; | |
| var show = function(){ | |
| if(enabled) | |
| $('#overlay').show(); | |
| } | |
| var hide = function(){ | |
| $('#overlay').fadeOut(); | |
| enable(); | |
| } | |
| var disable = function(){ | |
| enabled = false; | |
| } | |
| var enable = function(){ | |
| enabled = true; | |
| } | |
| return { | |
| show: show, | |
| hide: hide, | |
| enable: enable, | |
| disable: disable | |
| } | |
| }); | |
| angular.module('Spinner', ['overlay']) | |
| .factory('SpinnerInterceptor',['$q', 'overlay',function ($q, overlay) { | |
| return function (promise) { | |
| return promise.then(function (response) { | |
| overlay.hide(); | |
| return response; | |
| }, function (response) { | |
| overlay.hide(); | |
| return $q.reject(response); | |
| }); | |
| }; | |
| }]) | |
| .config(function ($httpProvider) { | |
| $httpProvider.responseInterceptors.push('SpinnerInterceptor'); | |
| var spinnerFunction = function (data) { | |
| var $injector = angular.element('#overlay').injector(); | |
| if($injector){ | |
| var $rootScope = $injector.get('$rootScope'); | |
| $rootScope.$broadcast('event:ajaxStart'); | |
| } | |
| return data; | |
| }; | |
| $httpProvider.defaults.transformRequest.push(spinnerFunction); | |
| }).run(function($rootScope, overlay){ | |
| $rootScope.$on('event:ajaxStart', function () { | |
| overlay.show(); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment