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

Commit 62d502d

Browse files
author
codekraft-studio
committed
upgrades in directive
better default validation function using scope string
1 parent 2dafc03 commit 62d502d

File tree

3 files changed

+57
-34
lines changed

3 files changed

+57
-34
lines changed

dist/angular-async-validation.js

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
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
*/
88
angular.module('angular-async-validation', []);
99

1010
angular.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

dist/angular-async-validation.min.js

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/angular-async-validation.directive.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ angular.module('angular-async-validation')
55
var directive = {
66
restrict: 'A',
77
require: 'ngModel',
8-
scope: { asyncValidation: '=' },
8+
scope: { asyncValidation: '=', validatorName: '@?' },
99
link: _link
1010
};
1111

@@ -14,7 +14,7 @@ angular.module('angular-async-validation')
1414
function _link(scope, elem, attrs, ngModel) {
1515

1616
// The async validation function
17-
var validationFunction;
17+
var validationFunction, validatorName = scope.validatorName ? scope.validatorName : 'asyncValidator';
1818

1919
// Check if the argument passed satisfy the requirements
2020
if( !scope.asyncValidation && !angular.isString(scope.asyncValidation) && !angular.isFunction(scope.asyncValidation) ) {
@@ -38,13 +38,13 @@ angular.module('angular-async-validation')
3838

3939
validationFunction = function (modelValue, viewValue) {
4040

41-
// get the value
41+
// get the current value
4242
var value = modelValue || viewValue;
4343

4444
// Consider empty models to be valid
4545
// for this type of validation
4646
if (ngModel.$isEmpty(value)) {
47-
return $q.resolve();;
47+
return $q.resolve();
4848
}
4949

5050
// Init the deferred object
@@ -54,14 +54,22 @@ angular.module('angular-async-validation')
5454
var url = scope.asyncValidation.replace(':value', value);
5555

5656
// run the request
57-
return $http.get(url, {
57+
$http.get(url, {
5858
notifyError: false
5959
}).then(function (response) {
60-
return $q.reject();
60+
61+
if( !response.data ) {
62+
deferred.resolve();
63+
} else {
64+
deferred.reject();
65+
}
66+
6167
}, function () {
62-
return $q.resolve();
68+
deferred.resolve();
6369
});
6470

71+
return deferred.promise;
72+
6573
};
6674

6775
}
@@ -72,8 +80,8 @@ angular.module('angular-async-validation')
7280
validationFunction = scope.asyncValidation;
7381
}
7482

75-
// Add the async validator
76-
ngModel.$asyncValidators.asyncValidation = validationFunction;
83+
// Add the async validator (optionally using custom name)
84+
ngModel.$asyncValidators[validatorName] = validationFunction;
7785

7886
}
7987

0 commit comments

Comments
 (0)