refactor(directive.ngModel): rename emitValidity -> setValidity

This commit is contained in:
Vojta Jina 2012-02-27 16:28:12 -08:00
parent 4e83399570
commit 4370d756e4
6 changed files with 52 additions and 51 deletions

View file

@ -297,9 +297,9 @@ This example shows how to implement a custom HTML editor widget in Angular.
ctrl.formatters.push(function(value) {
try {
value = $sanitize(value || '');
ctrl.emitValidity('HTML', true);
ctrl.setValidity('HTML', true);
} catch (e) {
ctrl.emitValidity('HTML', false);
ctrl.setValidity('HTML', false);
}
});

View file

@ -16,8 +16,8 @@
* - values are arrays of widgets that are invalid with given error.
*
* @description
* Form is a controller that keeps track of all registered widgets.
*
* `FormController` keeps track of all its widgets as well as state of them form, such as being valid/invalid or dirty/pristine.
*
* Each {@link angular.module.ng.$compileProvider.directive.form form} directive creates an instance
* of `FormController`.
*
@ -123,7 +123,7 @@ FormController.prototype.registerWidget = function(widget, alias) {
* @scope
* @description
* Directive that instantiates
* {@link angular.module.ng.$compileProvider.directive.form.FormController Form} controller.
* {@link angular.module.ng.$compileProvider.directive.form.FormController FormController}.
*
* If `name` attribute is specified, the controller is published to the scope as well.
*

View file

@ -391,10 +391,10 @@ function textInputType(scope, element, attr, ctrl) {
var emit = function(regexp, value) {
if (isEmpty(value) || regexp.test(value)) {
ctrl.emitValidity('PATTERN', true);
ctrl.setValidity('PATTERN', true);
return value;
} else {
ctrl.emitValidity('PATTERN', false);
ctrl.setValidity('PATTERN', false);
return undefined;
}
};
@ -425,10 +425,10 @@ function textInputType(scope, element, attr, ctrl) {
var minlength = parseInt(attr.ngMinlength, 10);
var minLengthValidator = function(value) {
if (!isEmpty(value) && value.length < minlength) {
ctrl.emitValidity('MINLENGTH', false);
ctrl.setValidity('MINLENGTH', false);
return undefined;
} else {
ctrl.emitValidity('MINLENGTH', true);
ctrl.setValidity('MINLENGTH', true);
return value;
}
};
@ -442,10 +442,10 @@ function textInputType(scope, element, attr, ctrl) {
var maxlength = parseInt(attr.ngMaxlength, 10);
var maxLengthValidator = function(value) {
if (!isEmpty(value) && value.length > maxlength) {
ctrl.emitValidity('MAXLENGTH', false);
ctrl.setValidity('MAXLENGTH', false);
return undefined;
} else {
ctrl.emitValidity('MAXLENGTH', true);
ctrl.setValidity('MAXLENGTH', true);
return value;
}
};
@ -461,10 +461,10 @@ function numberInputType(scope, element, attr, ctrl) {
ctrl.parsers.push(function(value) {
var empty = isEmpty(value);
if (empty || NUMBER_REGEXP.test(value)) {
ctrl.emitValidity('NUMBER', true);
ctrl.setValidity('NUMBER', true);
return value === '' ? null : (empty ? value : parseFloat(value));
} else {
ctrl.emitValidity('NUMBER', false);
ctrl.setValidity('NUMBER', false);
return undefined;
}
});
@ -477,10 +477,10 @@ function numberInputType(scope, element, attr, ctrl) {
var min = parseFloat(attr.min);
var minValidator = function(value) {
if (!isEmpty(value) && value < min) {
ctrl.emitValidity('MIN', false);
ctrl.setValidity('MIN', false);
return undefined;
} else {
ctrl.emitValidity('MIN', true);
ctrl.setValidity('MIN', true);
return value;
}
};
@ -493,10 +493,10 @@ function numberInputType(scope, element, attr, ctrl) {
var max = parseFloat(attr.max);
var maxValidator = function(value) {
if (!isEmpty(value) && value > max) {
ctrl.emitValidity('MAX', false);
ctrl.setValidity('MAX', false);
return undefined;
} else {
ctrl.emitValidity('MAX', true);
ctrl.setValidity('MAX', true);
return value;
}
};
@ -508,10 +508,10 @@ function numberInputType(scope, element, attr, ctrl) {
ctrl.formatters.push(function(value) {
if (isEmpty(value) || isNumber(value)) {
ctrl.emitValidity('NUMBER', true);
ctrl.setValidity('NUMBER', true);
return value;
} else {
ctrl.emitValidity('NUMBER', false);
ctrl.setValidity('NUMBER', false);
return undefined;
}
});
@ -522,10 +522,10 @@ function urlInputType(scope, element, attr, ctrl) {
var urlValidator = function(value) {
if (isEmpty(value) || URL_REGEXP.test(value)) {
ctrl.emitValidity('URL', true);
ctrl.setValidity('URL', true);
return value;
} else {
ctrl.emitValidity('URL', false);
ctrl.setValidity('URL', false);
return undefined;
}
};
@ -539,10 +539,10 @@ function emailInputType(scope, element, attr, ctrl) {
var emailValidator = function(value) {
if (isEmpty(value) || EMAIL_REGEXP.test(value)) {
ctrl.emitValidity('EMAIL', true);
ctrl.setValidity('EMAIL', true);
return value;
} else {
ctrl.emitValidity('EMAIL', false);
ctrl.setValidity('EMAIL', false);
return undefined;
}
};
@ -794,7 +794,7 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
/**
* @ngdoc function
* @name angular.module.ng.$compileProvider.directive.ng:model.NgModelController#emitValidity
* @name angular.module.ng.$compileProvider.directive.ng:model.NgModelController#setValidity
* @methodOf angular.module.ng.$compileProvider.directive.ng:model.NgModelController
*
* @description
@ -806,7 +806,7 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
* @param {string} name Name of the validator.
* @param {boolean} isValid Whether it should $emit `$valid` (true) or `$invalid` (false) event.
*/
this.emitValidity = function(name, isValid) {
this.setValidity = function(name, isValid) {
if (!isValid && this.error[name]) return;
if (isValid && !this.error[name]) return;
@ -890,7 +890,8 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
* @element input
*
* @description
* Is directive that tells Angular to do two-way data binding. It works together with `input`, `select`, `textarea`. You can easily write your own directives to use `ng:model` pretty easily.
* Is directive that tells Angular to do two-way data binding. It works together with `input`,
* `select`, `textarea`. You can easily write your own directives to use `ng:model` as well.
*
* `ng:model` is responsible for:
*
@ -901,7 +902,7 @@ var NgModelController = ['$scope', '$exceptionHandler', 'ngModel',
* - setting related css class onto the element (`ng-valid`, `ng-invalid`, `ng-dirty`, `ng-pristine`),
* - register the widget with parent {@link angular.module.ng.$compileProvider.directive.form form}.
*
* For examples, how to use `ng:model`, see:
* For basic examples, how to use `ng:model`, see:
*
* - {@link angular.module.ng.$compileProvider.directive.input input}
* - {@link angular.module.ng.$compileProvider.directive.input.text text}
@ -1080,10 +1081,10 @@ var requiredDirective = [function() {
var validator = function(value) {
if (attr.required && isEmpty(value)) {
ctrl.emitValidity('REQUIRED', false);
ctrl.setValidity('REQUIRED', false);
return null;
} else {
ctrl.emitValidity('REQUIRED', true);
ctrl.setValidity('REQUIRED', true);
return value;
}
};

View file

@ -139,7 +139,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
// required validator
if (multiple && (attr.required || attr.ngRequired)) {
var requiredValidator = function(value) {
ctrl.emitValidity('REQUIRED', !attr.required || (value && value.length));
ctrl.setValidity('REQUIRED', !attr.required || (value && value.length));
return value;
};

View file

@ -38,7 +38,7 @@ describe('form', function() {
'</form>')(scope);
var form = scope.form;
widget.emitValidity('REQUIRED', false);
widget.setValidity('REQUIRED', false);
expect(form.alias).toBe(widget);
expect(form.error.REQUIRED).toEqual([widget]);
@ -107,11 +107,11 @@ describe('form', function() {
var child = scope.child;
var input = child.text;
input.emitValidity('MyError', false);
input.setValidity('MyError', false);
expect(parent.error.MyError).toEqual([input]);
expect(child.error.MyError).toEqual([input]);
input.emitValidity('MyError', true);
input.setValidity('MyError', true);
expect(parent.error.MyError).toBeUndefined();
expect(child.error.MyError).toBeUndefined();
});
@ -138,12 +138,12 @@ describe('form', function() {
expect(child).toBeDefined();
expect(input).toBeDefined();
input.emitValidity('myRule', false);
input.setValidity('myRule', false);
expect(input.error.myRule).toEqual(true);
expect(child.error.myRule).toEqual([input]);
expect(parent.error.myRule).toEqual([input]);
input.emitValidity('myRule', true);
input.setValidity('myRule', true);
expect(parent.error.myRule).toBeUndefined();
expect(child.error.myRule).toBeUndefined();
});
@ -177,18 +177,18 @@ describe('form', function() {
it('should have ng-valid/ng-invalid css class', function() {
expect(doc).toBeValid();
widget.emitValidity('ERROR', false);
widget.setValidity('ERROR', false);
scope.$apply();
expect(doc).toBeInvalid();
widget.emitValidity('ANOTHER', false);
widget.setValidity('ANOTHER', false);
scope.$apply();
widget.emitValidity('ERROR', true);
widget.setValidity('ERROR', true);
scope.$apply();
expect(doc).toBeInvalid();
widget.emitValidity('ANOTHER', true);
widget.setValidity('ANOTHER', true);
scope.$apply();
expect(doc).toBeValid();
});

View file

@ -49,44 +49,44 @@ describe('NgModelController', function() {
});
describe('emitValidity', function() {
describe('setValidity', function() {
it('should emit $invalid only when $valid', function() {
var spy = jasmine.createSpy('$invalid');
scope.$on('$invalid', spy);
ctrl.emitValidity('ERROR', false);
ctrl.setValidity('ERROR', false);
expect(spy).toHaveBeenCalledOnce();
spy.reset();
ctrl.emitValidity('ERROR', false);
ctrl.setValidity('ERROR', false);
expect(spy).not.toHaveBeenCalled();
});
it('should set and unset the error', function() {
ctrl.emitValidity('REQUIRED', false);
ctrl.setValidity('REQUIRED', false);
expect(ctrl.error.REQUIRED).toBe(true);
ctrl.emitValidity('REQUIRED', true);
ctrl.setValidity('REQUIRED', true);
expect(ctrl.error.REQUIRED).toBeUndefined();
});
it('should set valid/invalid', function() {
ctrl.emitValidity('FIRST', false);
ctrl.setValidity('FIRST', false);
expect(ctrl.valid).toBe(false);
expect(ctrl.invalid).toBe(true);
ctrl.emitValidity('SECOND', false);
ctrl.setValidity('SECOND', false);
expect(ctrl.valid).toBe(false);
expect(ctrl.invalid).toBe(true);
ctrl.emitValidity('SECOND', true);
ctrl.setValidity('SECOND', true);
expect(ctrl.valid).toBe(false);
expect(ctrl.invalid).toBe(true);
ctrl.emitValidity('FIRST', true);
ctrl.setValidity('FIRST', true);
expect(ctrl.valid).toBe(true);
expect(ctrl.invalid).toBe(false);
});
@ -96,11 +96,11 @@ describe('NgModelController', function() {
var spy = jasmine.createSpy('$valid');
scope.$on('$valid', spy);
ctrl.emitValidity('ERROR', true);
ctrl.setValidity('ERROR', true);
expect(spy).not.toHaveBeenCalled();
ctrl.emitValidity('ERROR', false);
ctrl.emitValidity('ERROR', true);
ctrl.setValidity('ERROR', false);
ctrl.setValidity('ERROR', true);
expect(spy).toHaveBeenCalledOnce();
});
});