I am currently in the process of refactoring my code, with the intention of achieving the following structure:
Services:
} }]).factory('UsernameRESTService', ['Restangular',function(Restangular){
var restAngular =
Restangular.withConfig(function(Configurer) {
Configurer.setBaseUrl('http://localhost:2000/');
});
return {
getCarrier: function(carrierCode) {
var usernamePromise=restAngular.one(carrierCode).get();
return usernamePromise;
}
}}]).factory('UsernameValidationHelper',['UsernameRESTService','$q',function(UsernameRESTService,$q){
return{
usernameExists: function(username){
var deferred = $q.defer();
deffered.resolve(true);
return deferred.promise;
}
}}])
Directive:
angular.module('myApp.commonDirectives', ['myApp.services']).directive('username', ['UsernameValidationHelper','$q',function(UsernameValidationHelper,$q){
return {
restrict: 'A',
require: "ngModel",
link: function(scope, element, attrs, ctrl) {
ctrl.$asyncValidators.username = function(value){
var deferred = $q.defer();
bob= UsernameValidationHelper.usernameExists("bob");
defer.resolve();
return deferred.promise;
}
}
}
}])
Just experimenting at the moment due to my limited understanding of promises.
Upon calling
bob= UsernameValidationHelper.usernameExists("bob");
, I encounter a Type Error: Undefined is not a function
What could be my mistake?