refactor(forms): handle dirty/pristine explicitly
This commit is contained in:
parent
31b6687894
commit
96cadcc29e
|
@ -23,13 +23,14 @@ export function setUpControl(c: Control, dir: ControlDirective) {
|
||||||
dir.valueAccessor.registerOnChange(newValue => {
|
dir.valueAccessor.registerOnChange(newValue => {
|
||||||
dir.viewToModelUpdate(newValue);
|
dir.viewToModelUpdate(newValue);
|
||||||
c.updateValue(newValue);
|
c.updateValue(newValue);
|
||||||
|
c.markAsDirty();
|
||||||
});
|
});
|
||||||
|
|
||||||
// model -> view
|
// model -> view
|
||||||
c.registerOnChange(newValue => dir.valueAccessor.writeValue(newValue));
|
c.registerOnChange(newValue => dir.valueAccessor.writeValue(newValue));
|
||||||
|
|
||||||
// touched
|
// touched
|
||||||
dir.valueAccessor.registerOnTouched(() => c.touch());
|
dir.valueAccessor.registerOnTouched(() => c.markAsTouched());
|
||||||
}
|
}
|
||||||
|
|
||||||
function _throwError(dir: ControlDirective, message: string): void {
|
function _throwError(dir: ControlDirective, message: string): void {
|
||||||
|
|
|
@ -60,7 +60,16 @@ export class AbstractControl {
|
||||||
|
|
||||||
get valueChanges(): Observable { return this._valueChanges; }
|
get valueChanges(): Observable { return this._valueChanges; }
|
||||||
|
|
||||||
touch(): void { this._touched = true; }
|
markAsTouched(): void { this._touched = true; }
|
||||||
|
|
||||||
|
markAsDirty({onlySelf}: {onlySelf?: boolean} = {}): void {
|
||||||
|
onlySelf = isPresent(onlySelf) ? onlySelf : false;
|
||||||
|
|
||||||
|
this._pristine = false;
|
||||||
|
if (isPresent(this._parent) && !onlySelf) {
|
||||||
|
this._parent.markAsDirty({onlySelf: onlySelf});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setParent(parent) { this._parent = parent; }
|
setParent(parent) { this._parent = parent; }
|
||||||
|
|
||||||
|
@ -80,7 +89,6 @@ export class AbstractControl {
|
||||||
emitEvent = isPresent(emitEvent) ? emitEvent : true;
|
emitEvent = isPresent(emitEvent) ? emitEvent : true;
|
||||||
|
|
||||||
this._updateValue();
|
this._updateValue();
|
||||||
this._pristine = false;
|
|
||||||
|
|
||||||
if (emitEvent) {
|
if (emitEvent) {
|
||||||
ObservableWrapper.callNext(this._valueChanges, this._value);
|
ObservableWrapper.callNext(this._valueChanges, this._value);
|
||||||
|
|
|
@ -39,19 +39,6 @@ export function main() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("pristine", () => {
|
|
||||||
it("should be true after creating a control", () => {
|
|
||||||
var c = new Control("value");
|
|
||||||
expect(c.pristine).toEqual(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should be false after changing the value of the control", () => {
|
|
||||||
var c = new Control("value");
|
|
||||||
c.updateValue("new value");
|
|
||||||
expect(c.pristine).toEqual(false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("dirty", () => {
|
describe("dirty", () => {
|
||||||
it("should be false after creating a control", () => {
|
it("should be false after creating a control", () => {
|
||||||
var c = new Control("value");
|
var c = new Control("value");
|
||||||
|
@ -60,7 +47,7 @@ export function main() {
|
||||||
|
|
||||||
it("should be true after changing the value of the control", () => {
|
it("should be true after changing the value of the control", () => {
|
||||||
var c = new Control("value");
|
var c = new Control("value");
|
||||||
c.updateValue("new value");
|
c.markAsDirty();
|
||||||
expect(c.dirty).toEqual(true);
|
expect(c.dirty).toEqual(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -215,20 +202,22 @@ export function main() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("pristine", () => {
|
describe("dirty", () => {
|
||||||
it("should be true after creating a control", () => {
|
var c,g;
|
||||||
var c = new Control('value');
|
|
||||||
var g = new ControlGroup({"one": c});
|
|
||||||
|
|
||||||
expect(g.pristine).toEqual(true);
|
beforeEach(() => {
|
||||||
|
c = new Control('value');
|
||||||
|
g = new ControlGroup({"one": c});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should be false after creating a control", () => {
|
||||||
|
expect(g.dirty).toEqual(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should be false after changing the value of the control", () => {
|
it("should be false after changing the value of the control", () => {
|
||||||
var c = new Control('value');
|
c.markAsDirty();
|
||||||
var g = new ControlGroup({"one": c});
|
|
||||||
c.updateValue('new value');
|
|
||||||
|
|
||||||
expect(g.pristine).toEqual(false);
|
expect(g.dirty).toEqual(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -452,19 +441,22 @@ export function main() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("pristine", () => {
|
describe("dirty", () => {
|
||||||
it("should be true after creating a control", () => {
|
var c,a;
|
||||||
var a = new ControlArray([new Control(1)]);
|
|
||||||
expect(a.pristine).toBe(true);
|
beforeEach(() => {
|
||||||
|
c = new Control('value');
|
||||||
|
a = new ControlArray([c]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should be false after creating a control", () => {
|
||||||
|
expect(a.dirty).toEqual(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should be false after changing the value of the control", () => {
|
it("should be false after changing the value of the control", () => {
|
||||||
var c = new Control(1);
|
c.markAsDirty();
|
||||||
var a = new ControlArray([c]);
|
|
||||||
|
|
||||||
c.updateValue('new value');
|
expect(a.dirty).toEqual(true);
|
||||||
|
|
||||||
expect(a.pristine).toEqual(false);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue