forked from webadvanced/ng-remote-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Web Advanced
committed
Apr 25, 2014
1 parent
de5c895
commit a125f4a
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
( function( angular ) { | ||
'use strict'; | ||
if( !angular ) { | ||
throw 'Missing something? Please add angular.js to your project or move this script below the angular.js reference'; | ||
} | ||
|
||
var directiveId = 'ngRemoteValidate', | ||
remoteValidate = function( $http, $timeout ) { | ||
return { | ||
restrict: 'A', | ||
require: 'ngModel', | ||
link: function( scope, el, attrs, ngModel ) { | ||
var cache = {}, | ||
handleChange, | ||
setValidation, | ||
addToCache, | ||
originalValue, | ||
request, | ||
shouldProcess, | ||
options = { | ||
ngRemoteThrottle: 400, | ||
ngRemoteMethod: 'POST' | ||
}; | ||
|
||
angular.extend( options, attrs ); | ||
|
||
addToCache = function( data ) { | ||
if ( cache[ data.value ] ) return; | ||
cache[ data.value ] = data.valid; | ||
}; | ||
|
||
shouldProcess = function( value ) { | ||
var otherRulesInValid = false; | ||
for ( var p in ngModel.$error ) { | ||
if ( ngModel.$error[ p ] && p != directiveId ) { | ||
otherRulesInValid = true; | ||
break; | ||
} | ||
} | ||
return !( ngModel.$pristine || value === originalValue || otherRulesInValid ); | ||
}; | ||
|
||
setValidation = function( data ) { | ||
ngModel.$setValidity( directiveId, data.isValid ); | ||
addToCache( data ); | ||
el.removeClass( 'ng-processing' ); | ||
}; | ||
|
||
handleChange = function( value ) { | ||
|
||
originalValue = originalValue || value; | ||
|
||
if ( !shouldProcess( value ) ) { | ||
return setValidation( { isValid: true, value: value } ); | ||
} | ||
|
||
if ( cache[ value ] ) { | ||
return setValidation( cache[ value ] ); | ||
} | ||
|
||
if ( request ) { | ||
$timeout.cancel( request ); | ||
} | ||
|
||
request = $timeout( function( ) { | ||
el.addClass( 'ng-processing' ); | ||
$http( { method: options.ngRemoteMethod, url: options.ngRemoteValidate, data: { value: value } } ).success( setValidation ); | ||
}, options.ngRemoteThrottle ); | ||
return true; | ||
}; | ||
|
||
scope.$watch( function( ) { | ||
return ngModel.$viewValue; | ||
}, handleChange ); | ||
} | ||
}; | ||
}; | ||
|
||
angular.module( 'remoteValidation', [] ) | ||
.constant('MODULE_VERSION', '0.1.3') | ||
.directive( directiveId, [ '$http', '$timeout', remoteValidate ] ); | ||
|
||
})( this.angular ); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.