11/**
2- * Package: angular-async-validation - v0.0.2
2+ * Package: angular-async-validation - v0.0.4
33* Description: A multi purpose directive for input async validation
4- * Last build: 2016-12-29
4+ * Last build: 2017-05-04
55* @author codekraft-studio
66* @license ISC
77*/
88angular . module ( 'angular-async-validation' , [ ] ) ;
99
1010angular . module ( 'angular-async-validation' )
1111
12- . directive ( 'asyncValidation' , [ '$log' , '$http' , function ( $log , $http ) {
12+ . directive ( 'asyncValidation' , [ '$log' , '$q' , '$ http', function ( $log , $q , $http ) {
1313
1414 var directive = {
1515 restrict : 'A' ,
1616 require : 'ngModel' ,
17+ scope : { asyncValidation : '=' , validatorName : '@?' } ,
1718 link : _link
1819 } ;
1920
@@ -22,18 +23,15 @@ angular.module('angular-async-validation')
2223 function _link ( scope , elem , attrs , ngModel ) {
2324
2425 // The async validation function
25- var validationFunction ;
26+ var validationFunction , validatorName = scope . validatorName ? scope . validatorName : 'asyncValidator' ;
2627
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 ) ) {
3130 $log . warn ( 'angular-async-validation: missing or empty argument in async-validation attribute on:' , elem ) ;
3231 return ;
3332 }
3433
35- // If no options are specified
36- // set to the defaults
34+ // If no options are specified set to the defaults
3735 if ( ! ngModel . $options || ! ngModel . $options . getOption ( 'debounce' ) ) {
3836
3937 ngModel . $options = ngModel . $options . createChild ( {
@@ -44,39 +42,55 @@ angular.module('angular-async-validation')
4442
4543 }
4644
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 ) ) {
5047
5148 validationFunction = function ( modelValue , viewValue ) {
5249
53- // get the value
50+ // get the current value
5451 var value = modelValue || viewValue ;
5552
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+
5662 // build the url
57- var url = asyncValidation . replace ( ':value' , value ) ;
63+ var url = scope . asyncValidation . replace ( ':value' , value ) ;
5864
5965 // run the request
60- return $http . get ( url , {
66+ $http . get ( url , {
6167 notifyError : false
6268 } ) . then ( function ( response ) {
63- return $q . reject ( ) ;
69+
70+ if ( ! response . data ) {
71+ deferred . resolve ( ) ;
72+ } else {
73+ deferred . reject ( ) ;
74+ }
75+
6476 } , function ( ) {
65- return $q . resolve ( ) ;
77+ deferred . resolve ( ) ;
6678 } ) ;
6779
80+ return deferred . promise ;
81+
6882 } ;
6983
7084 }
7185
7286 // If is a function defined by users
7387 // assign it to asyncValidators
74- if ( angular . isFunction ( asyncValidation ) ) {
75- validationFunction = asyncValidation ;
88+ if ( angular . isFunction ( scope . asyncValidation ) ) {
89+ validationFunction = scope . asyncValidation ;
7690 }
7791
78- // Add the async validator
79- ngModel . $asyncValidators . asyncValidation = validationFunction ;
92+ // Add the async validator (optionally using custom name)
93+ ngModel . $asyncValidators [ validatorName ] = validationFunction ;
8094
8195 }
8296
0 commit comments