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.viewToModelUpdate(newValue);
|
||||
c.updateValue(newValue);
|
||||
c.markAsDirty();
|
||||
});
|
||||
|
||||
// model -> view
|
||||
c.registerOnChange(newValue => dir.valueAccessor.writeValue(newValue));
|
||||
|
||||
// touched
|
||||
dir.valueAccessor.registerOnTouched(() => c.touch());
|
||||
dir.valueAccessor.registerOnTouched(() => c.markAsTouched());
|
||||
}
|
||||
|
||||
function _throwError(dir: ControlDirective, message: string): void {
|
||||
|
|
|
@ -60,7 +60,16 @@ export class AbstractControl {
|
|||
|
||||
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; }
|
||||
|
||||
|
@ -80,7 +89,6 @@ export class AbstractControl {
|
|||
emitEvent = isPresent(emitEvent) ? emitEvent : true;
|
||||
|
||||
this._updateValue();
|
||||
this._pristine = false;
|
||||
|
||||
if (emitEvent) {
|
||||
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", () => {
|
||||
it("should be false after creating a control", () => {
|
||||
var c = new Control("value");
|
||||
|
@ -60,7 +47,7 @@ export function main() {
|
|||
|
||||
it("should be true after changing the value of the control", () => {
|
||||
var c = new Control("value");
|
||||
c.updateValue("new value");
|
||||
c.markAsDirty();
|
||||
expect(c.dirty).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
@ -215,20 +202,22 @@ export function main() {
|
|||
});
|
||||
});
|
||||
|
||||
describe("pristine", () => {
|
||||
it("should be true after creating a control", () => {
|
||||
var c = new Control('value');
|
||||
var g = new ControlGroup({"one": c});
|
||||
describe("dirty", () => {
|
||||
var c,g;
|
||||
|
||||
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", () => {
|
||||
var c = new Control('value');
|
||||
var g = new ControlGroup({"one": c});
|
||||
c.updateValue('new value');
|
||||
c.markAsDirty();
|
||||
|
||||
expect(g.pristine).toEqual(false);
|
||||
expect(g.dirty).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -452,19 +441,22 @@ export function main() {
|
|||
});
|
||||
});
|
||||
|
||||
describe("pristine", () => {
|
||||
it("should be true after creating a control", () => {
|
||||
var a = new ControlArray([new Control(1)]);
|
||||
expect(a.pristine).toBe(true);
|
||||
describe("dirty", () => {
|
||||
var c,a;
|
||||
|
||||
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", () => {
|
||||
var c = new Control(1);
|
||||
var a = new ControlArray([c]);
|
||||
c.markAsDirty();
|
||||
|
||||
c.updateValue('new value');
|
||||
|
||||
expect(a.pristine).toEqual(false);
|
||||
expect(a.dirty).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue