Created
June 16, 2014 12:27
-
-
Save cpoDesign/9acd73f9a0e166d39667 to your computer and use it in GitHub Desktop.
Generic implementation for loader. It will target root scope while any promise is in process
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
<p> | |
Implementation of loader in $rootScope | |
</p> | |
<div style="float:right;padding-top:1em;" data-ng-show="loading"> | |
<img src="~/images/ajax-loader-inline.gif" /> Loading, please wait ... | |
</div> |
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
angular.module('myModule', []) | |
.config(function ($provide) { | |
// display loading if something takes too long | |
$provide.decorator('$q', ['$delegate', '$rootScope', function ($delegate, $rootScope) { | |
var pendingPromisses = 0; | |
$rootScope.$watch( | |
function () { return pendingPromisses > 0; }, | |
function (loading) { $rootScope.loading = loading; } | |
); | |
var $q = $delegate; | |
var origDefer = $q.defer; | |
$q.defer = function () { | |
var defer = origDefer(); | |
pendingPromisses++; | |
defer.promise.finally(function () { | |
pendingPromisses--; | |
}); | |
return defer; | |
}; | |
return $q; | |
}]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment