1
1
/**
2
- * Package: angular-async-validation - v0.0.2
2
+ * Package: angular-async-validation - v0.0.4
3
3
* Description: A multi purpose directive for input async validation
4
- * Last build: 2016-12-29
4
+ * Last build: 2017-05-04
5
5
* @author codekraft-studio
6
6
* @license ISC
7
7
*/
8
8
angular . module ( 'angular-async-validation' , [ ] ) ;
9
9
10
10
angular . module ( 'angular-async-validation' )
11
11
12
- . directive ( 'asyncValidation' , [ '$log' , '$http' , function ( $log , $http ) {
12
+ . directive ( 'asyncValidation' , [ '$log' , '$q' , '$ http', function ( $log , $q , $http ) {
13
13
14
14
var directive = {
15
15
restrict : 'A' ,
16
16
require : 'ngModel' ,
17
+ scope : { asyncValidation : '=' , validatorName : '@?' } ,
17
18
link : _link
18
19
} ;
19
20
@@ -22,18 +23,15 @@ angular.module('angular-async-validation')
22
23
function _link ( scope , elem , attrs , ngModel ) {
23
24
24
25
// The async validation function
25
- var validationFunction ;
26
+ var validationFunction , validatorName = scope . validatorName ? scope . validatorName : 'asyncValidator' ;
26
27
27
- // The attribute value
28
- var asyncValidation = scope . $eval ( attrs . asyncValidation ) ;
29
-
30
- if ( ! asyncValidation || ! asyncValidation . length || typeof asyncValidation === 'undefined' ) {
28
+ // Check if the argument passed satisfy the requirements
29
+ if ( ! scope . asyncValidation && ! angular . isString ( scope . asyncValidation ) && ! angular . isFunction ( scope . asyncValidation ) ) {
31
30
$log . warn ( 'angular-async-validation: missing or empty argument in async-validation attribute on:' , elem ) ;
32
31
return ;
33
32
}
34
33
35
- // If no options are specified
36
- // set to the defaults
34
+ // If no options are specified set to the defaults
37
35
if ( ! ngModel . $options || ! ngModel . $options . getOption ( 'debounce' ) ) {
38
36
39
37
ngModel . $options = ngModel . $options . createChild ( {
@@ -44,39 +42,55 @@ angular.module('angular-async-validation')
44
42
45
43
}
46
44
47
- // If is a string use it as
48
- // path for ajax request
49
- if ( angular . isString ( asyncValidation ) ) {
45
+ // If is a string use it as path for http request
46
+ if ( angular . isString ( scope . asyncValidation ) ) {
50
47
51
48
validationFunction = function ( modelValue , viewValue ) {
52
49
53
- // get the value
50
+ // get the current value
54
51
var value = modelValue || viewValue ;
55
52
53
+ // Consider empty models to be valid
54
+ // for this type of validation
55
+ if ( ngModel . $isEmpty ( value ) ) {
56
+ return $q . resolve ( ) ;
57
+ }
58
+
59
+ // Init the deferred object
60
+ var deferred = $q . defer ( ) ;
61
+
56
62
// build the url
57
- var url = asyncValidation . replace ( ':value' , value ) ;
63
+ var url = scope . asyncValidation . replace ( ':value' , value ) ;
58
64
59
65
// run the request
60
- return $http . get ( url , {
66
+ $http . get ( url , {
61
67
notifyError : false
62
68
} ) . then ( function ( response ) {
63
- return $q . reject ( ) ;
69
+
70
+ if ( ! response . data ) {
71
+ deferred . resolve ( ) ;
72
+ } else {
73
+ deferred . reject ( ) ;
74
+ }
75
+
64
76
} , function ( ) {
65
- return $q . resolve ( ) ;
77
+ deferred . resolve ( ) ;
66
78
} ) ;
67
79
80
+ return deferred . promise ;
81
+
68
82
} ;
69
83
70
84
}
71
85
72
86
// If is a function defined by users
73
87
// assign it to asyncValidators
74
- if ( angular . isFunction ( asyncValidation ) ) {
75
- validationFunction = asyncValidation ;
88
+ if ( angular . isFunction ( scope . asyncValidation ) ) {
89
+ validationFunction = scope . asyncValidation ;
76
90
}
77
91
78
- // Add the async validator
79
- ngModel . $asyncValidators . asyncValidation = validationFunction ;
92
+ // Add the async validator (optionally using custom name)
93
+ ngModel . $asyncValidators [ validatorName ] = validationFunction ;
80
94
81
95
}
82
96
0 commit comments