cleanup(forms): cleanup

This commit is contained in:
vsavkin 2015-06-17 15:42:07 -07:00
parent 4d1ed509e3
commit 5ba5da5d25
8 changed files with 22 additions and 39 deletions

View File

@ -30,12 +30,10 @@ import {setProperty} from './shared';
})
export class CheckboxControlValueAccessor implements ControlValueAccessor {
checked: boolean;
onChange: Function;
onTouched: Function;
onChange = (_) => {};
onTouched = () => {};
constructor(private cd: NgControl, private renderer: Renderer, private elementRef: ElementRef) {
this.onChange = (_) => {};
this.onTouched = (_) => {};
cd.valueAccessor = this;
}

View File

@ -33,12 +33,10 @@ import {setProperty} from './shared';
})
export class DefaultValueAccessor implements ControlValueAccessor {
value: string = null;
onChange: Function;
onTouched: Function;
onChange = (_) => {};
onTouched = () => {};
constructor(private cd: NgControl, private renderer: Renderer, private elementRef: ElementRef) {
this.onChange = (_) => {};
this.onTouched = (_) => {};
cd.valueAccessor = this;
}

View File

@ -75,25 +75,23 @@ const controlNameBinding =
@Directive({
selector: '[ng-control]',
hostInjector: [controlNameBinding],
properties: ['name: ng-control', 'model: ng-model'],
events: ['ngModel'],
properties: ['name: ngControl', 'model: ngModel'],
events: ['update: ngModel'],
lifecycle: [onDestroy, onChange],
exportAs: 'form'
})
export class NgControlName extends NgControl {
_parent: ControlContainer;
ngModel: EventEmitter;
update = new EventEmitter();
model: any;
ngValidators: QueryList<NgValidator>;
_added: boolean;
_added = false;
// Scope the query once https://github.com/angular/angular/issues/2603 is fixed
constructor(@Ancestor() parent: ControlContainer,
@Query(NgValidator) ngValidators: QueryList<NgValidator>) {
super();
this._parent = parent;
this.ngModel = new EventEmitter();
this._added = false;
this.ngValidators = ngValidators;
}
@ -109,7 +107,7 @@ export class NgControlName extends NgControl {
onDestroy() { this.formDirective.removeControl(this); }
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.ngModel, newValue); }
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.update, newValue); }
get path(): List<string> { return controlPath(this.name, this._parent); }

View File

@ -63,23 +63,21 @@ const formControlBinding =
@Directive({
selector: '[ng-form-control]',
hostInjector: [formControlBinding],
properties: ['form: ng-form-control', 'model: ng-model'],
events: ['ngModel'],
properties: ['form: ngFormControl', 'model: ngModel'],
events: ['update: ngModel'],
lifecycle: [onChange],
exportAs: 'form'
})
export class NgFormControl extends NgControl {
form: Control;
ngModel: EventEmitter;
_added: boolean;
update = new EventEmitter();
_added = false;
model: any;
ngValidators: QueryList<NgValidator>;
// Scope the query once https://github.com/angular/angular/issues/2603 is fixed
constructor(@Query(NgValidator) ngValidators: QueryList<NgValidator>) {
super();
this.ngModel = new EventEmitter();
this._added = false;
this.ngValidators = ngValidators;
}
@ -100,5 +98,5 @@ export class NgFormControl extends NgControl {
get validator(): Function { return composeNgValidator(this.ngValidators); }
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.ngModel, newValue); }
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.update, newValue); }
}

View File

@ -95,14 +95,9 @@ const formDirectiveBinding =
})
export class NgFormModel extends ControlContainer implements Form {
form: ControlGroup = null;
directives: List<NgControl>;
directives: List<NgControl> = [];
ngSubmit = new EventEmitter();
constructor() {
super();
this.directives = [];
}
onChange(_) { this._updateDomValue(); }
get formDirective(): Form { return this; }

View File

@ -33,15 +33,15 @@ const formControlBinding = CONST_EXPR(new Binding(NgControl, {toAlias: forwardRe
@Directive({
selector: '[ng-model]:not([ng-control]):not([ng-form-control])',
hostInjector: [formControlBinding],
properties: ['model: ng-model'],
events: ['ngModel'],
properties: ['model: ngModel'],
events: ['update: ngModel'],
lifecycle: [onChange],
exportAs: 'form'
})
export class NgModel extends NgControl {
_control = new Control("");
_added = false;
ngModel = new EventEmitter();
update = new EventEmitter();
model: any;
ngValidators: QueryList<NgValidator>;
@ -69,5 +69,5 @@ export class NgModel extends NgControl {
get validator(): Function { return composeNgValidator(this.ngValidators); }
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.ngModel, newValue); }
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.update, newValue); }
}

View File

@ -40,13 +40,11 @@ export class NgSelectOption {
})
export class SelectControlValueAccessor implements ControlValueAccessor {
value = '';
onChange: Function;
onTouched: Function;
onChange = (_) => {};
onTouched = () => {};
constructor(private cd: NgControl, private renderer: Renderer, private elementRef: ElementRef,
@Query(NgSelectOption, {descendants: true}) query: QueryList<NgSelectOption>) {
this.onChange = (_) => {};
this.onTouched = (_) => {};
cd.valueAccessor = this;
this._updateValueWhenListOfOptionsChanges(query);

View File

@ -35,9 +35,7 @@ class CheckoutModel {
*/
@Directive({selector: '[credit-card]'})
class CreditCardValidator {
constructor(c: NgControl) {
c.validator = Validators.compose([c.validator, CreditCardValidator.validate]);
}
get validator() { return CreditCardValidator.validate; }
static validate(c): StringMap<string, boolean> {
if (isPresent(c.value) && RegExpWrapper.test(new RegExp("^\\d{16}$"), c.value)) {