Created
December 28, 2013 04:17
-
-
Save ejholmes/8156063 to your computer and use it in GitHub Desktop.
ngSubmitLoading
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
(function(angular) { | |
'use strict'; | |
var module = angular.module('ngSubmitLoading', []); | |
/** | |
* A directive that adds a `$loading` attribute to the ngForm controller | |
* until the promise is fullfilled. | |
* | |
* @example | |
* | |
* <form name="form" ng-submit-loading="fnThatReturnsPromise()"> | |
* <button ng-disabled="form.$loading"> | |
* Submit | |
* </button> | |
* </form> | |
*/ | |
module.directive('ngSubmitLoading', function($parse) { | |
return { | |
require: '^form', | |
compile: function($element, attr) { | |
var fn = $parse(attr.ngSubmitLoading); | |
return function(scope, element, attr, ctrl) { | |
element.on('submit', function(event) { | |
scope.$apply(function() { | |
ctrl.$loading = true; | |
fn(scope, { $event: event }).finally(function() { | |
ctrl.$loading = false; | |
}); | |
}); | |
}); | |
}; | |
} | |
}; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment