feat(form): implemented an imperative way of updating the view by updating the value of a control

This commit is contained in:
vsavkin
2015-06-01 10:41:50 -07:00
parent 559f54e92b
commit 652ed0cf6d
9 changed files with 145 additions and 75 deletions

View File

@ -1,4 +1,5 @@
import {CONST_EXPR} from 'angular2/src/facade/lang';
import {StringMapWrapper} from 'angular2/src/facade/collection';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {Directive, Ancestor, onChange} from 'angular2/angular2';
@ -53,20 +54,24 @@ const formControlBinding =
export class FormControlDirective extends ControlDirective {
control: Control;
ngModel: EventEmitter;
_added: boolean;
model: any;
constructor() {
super();
this.ngModel = new EventEmitter();
this._added = false;
}
onChange(_) {
setUpControl(this.control, this);
this.control.updateValidity();
}
set model(value) {
this.control.updateValue(value);
this.valueAccessor.writeValue(value);
onChange(c) {
if (!this._added) {
setUpControl(this.control, this);
this.control.updateValidity();
this._added = true;
}
if (StringMapWrapper.contains(c, "model")) {
this.control.updateValue(this.model);
}
}
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.ngModel, newValue); }

View File

@ -89,8 +89,7 @@ export class FormModelDirective extends ControlContainerDirective implements For
updateModel(dir: ControlDirective, value: any): void {
var c  = <Control>this.form.find(dir.path);
c.value = value;
dir.valueAccessor.writeValue(value);
c.updateValue(value);
}
_updateDomValue() {

View File

@ -40,8 +40,7 @@ export class NgModelDirective extends ControlDirective {
};
if (StringMapWrapper.contains(c, "model")) {
this.control.value = this.model;
this.valueAccessor.writeValue(this.model);
this.control.updateValue(this.model);
}
}

View File

@ -18,10 +18,15 @@ export function setUpControl(c: Control, dir: ControlDirective) {
c.validator = Validators.compose([c.validator, dir.validator]);
dir.valueAccessor.writeValue(c.value);
// view -> model
dir.valueAccessor.registerOnChange(newValue => {
dir.viewToModelUpdate(newValue);
c.updateValue(newValue);
});
// model -> view
c.registerOnChange(newValue => dir.valueAccessor.writeValue(newValue));
}
function _throwError(dir: ControlDirective, message: string): void {

View File

@ -65,8 +65,7 @@ export class TemplateDrivenFormDirective extends ControlContainerDirective imple
updateModel(dir: ControlDirective, value: any): void {
this._later(_ => {
var c = <Control>this.form.find(dir.path);
c.value = value;
dir.valueAccessor.writeValue(value);
c.updateValue(value);
});
}