Last active
August 29, 2015 13:57
-
-
Save Deraen/9811452 to your computer and use it in GitHub Desktop.
angular-utils.js
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('fooApp') | |
| .factory('Util', function() { | |
| var utils = {}; | |
| // Would be passed by value if $scope.foo was given directly. | |
| // As scope is object it is passed by reference. | |
| utils.Pending = function(scope, target) { | |
| scope[target] = false; | |
| this.start = function() { | |
| scope[target] = true; | |
| }; | |
| this.stop = function() { | |
| scope[target] = false; | |
| }; | |
| }; | |
| utils.Error = function(scope, target) { | |
| scope[target] = null; | |
| this.clear = function() { | |
| scope[target] = null; | |
| }; | |
| this.set = function(err) { | |
| scope[target] = err; | |
| }; | |
| }; | |
| return utils; | |
| }); |
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
| <button class="btn btn-success" ng-click="doFoo()" ng-class="{'spinner-active': fooPending}"> | |
| <span class="spinner glyphicon glyphicon-save"></span> Foo | |
| </button> | |
| <div error="fooError"></div> |
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('fooApp') | |
| .controller(function($scope, $http, Util) { | |
| var error = new Util.Error($scope, 'fooError'); | |
| var pending = new Util.Pending($scope, 'fooPending'); | |
| $scope.doFoo = function() { | |
| error.clear(); | |
| pending.start(); | |
| $http.get('/foo').success(function(data) { ... }).error(error.set).finally(pending.stop); | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment