1

I have and angular module angular.validators that is a wrapper for validator.js. Up until now every time validator.js was updated I would copy the source code and paste it on my service and then make it available on my directives.

I decided to automate the procedure and replaced the service code with the following:

.service('validator', ['$http', '$q', 'validator_version', function ($http, $q, version) {
    var deferred = $q.defer();
    $http.get('https://cdn.rawgit.com/chriso/validator.js/'+ version + '/validator.js')
        .then(function(result){
            deferred.resolve(result.data);
        });
    return deferred.promise;
}])

I know that it should not work, because validator returns a promise and on my directives I treat it like a regular function, e.g:

.directive('contains', ['validator', function (validator) {
    return {
        require: 'ngModel',
        restrict: 'A',
        link: function (scope, element, attrs, controller) {
            controller.$validators.contains = function (modelValue, viewValue) {
                if (controller.$isEmpty(modelValue)) {
                    return true;
                }
                return validator.contains(viewValue, attrs.contains);
            };
        }
    };
}])

but when i run my karma tests, oddly all tests pass without issues (a total of 81 tests atm). I thought that this is odd and then created a test project for my module and indeed when I ran it live on the browser all directives failed since they were not available. Why could this behaviour be?

Update: I've moved the odd code to a new branch, here.

gkaimakas
  • 574
  • 3
  • 17

0 Answers0