Skip to content
This repository was archived by the owner on Feb 2, 2019. It is now read-only.

Commit 81f4302

Browse files
author
codekraft-studio
committed
link function fixes
1 parent 66c2e6c commit 81f4302

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/angular-async-validation.directive.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
angular.module('angular-async-validation')
22

3-
.directive('asyncValidation', ['$log', '$http', function ($log, $http) {
3+
.directive('asyncValidation', ['$log', '$q', '$http', function ($log, $q, $http) {
44

55
var directive = {
66
restrict: 'A',
77
require: 'ngModel',
8+
scope: { asyncValidation: '=' },
89
link: _link
910
};
1011

@@ -15,16 +16,13 @@ angular.module('angular-async-validation')
1516
// The async validation function
1617
var validationFunction;
1718

18-
// The attribute value
19-
var asyncValidation = scope.$eval(attrs.asyncValidation);
20-
21-
if( !asyncValidation || !asyncValidation.length || typeof asyncValidation === 'undefined' ) {
19+
// Check if the argument passed satisfy the requirements
20+
if( !scope.asyncValidation && !angular.isString(scope.asyncValidation) && !angular.isFunction(scope.asyncValidation) ) {
2221
$log.warn('angular-async-validation: missing or empty argument in async-validation attribute on:', elem);
2322
return;
2423
}
2524

26-
// If no options are specified
27-
// set to the defaults
25+
// If no options are specified set to the defaults
2826
if( !ngModel.$options || !ngModel.$options.getOption('debounce') ) {
2927

3028
ngModel.$options = ngModel.$options.createChild({
@@ -35,17 +33,25 @@ angular.module('angular-async-validation')
3533

3634
}
3735

38-
// If is a string use it as
39-
// path for ajax request
40-
if( angular.isString(asyncValidation) ) {
36+
// If is a string use it as path for http request
37+
if( angular.isString(scope.asyncValidation) ) {
4138

4239
validationFunction = function (modelValue, viewValue) {
4340

4441
// get the value
4542
var value = modelValue || viewValue;
4643

44+
// Consider empty models to be valid
45+
// for this type of validation
46+
if (ngModel.$isEmpty(value)) {
47+
return $q.resolve();;
48+
}
49+
50+
// Init the deferred object
51+
var deferred = $q.defer();
52+
4753
// build the url
48-
var url = asyncValidation.replace(':value', value);
54+
var url = scope.asyncValidation.replace(':value', value);
4955

5056
// run the request
5157
return $http.get(url, {
@@ -62,8 +68,8 @@ angular.module('angular-async-validation')
6268

6369
// If is a function defined by users
6470
// assign it to asyncValidators
65-
if( angular.isFunction(asyncValidation) ) {
66-
validationFunction = asyncValidation;
71+
if( angular.isFunction(scope.asyncValidation) ) {
72+
validationFunction = scope.asyncValidation;
6773
}
6874

6975
// Add the async validator

0 commit comments

Comments
 (0)