Skip to content

Commit

Permalink
Merge pull request #32 from cloudnc/feat/allow-extending-classes-to-c…
Browse files Browse the repository at this point in the history
…ontrol-behaviors

feat(Inheritors): Allow descendant classes to control the init and destroy emission behaviors
  • Loading branch information
zakhenry authored Apr 23, 2019
2 parents fcc57a5 + 8952f95 commit 0124273
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions projects/ngx-sub-form/src/lib/ngx-sub-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont

protected onChange: Function | undefined = undefined;
protected onTouched: Function | undefined = undefined;
protected emitNullOnDestroy = true;
protected emitInitialValueOnInit = true;

private subscription: Subscription | undefined = undefined;

Expand Down Expand Up @@ -113,7 +115,7 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont
this.subscription.unsubscribe();
}

if (this.onChange) {
if (this.emitNullOnDestroy && this.onChange) {
this.onChange(null);
}

Expand Down Expand Up @@ -159,11 +161,13 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont

// this is required to correctly initialize the form value
// see note on-change-after-one-tick within the test file for more info
setTimeout(() => {
if (this.onChange && this.formGroup) {
this.onChange(this.transformFromFormGroup(this.formGroup.value));
}
}, 0);
if (this.emitInitialValueOnInit) {
setTimeout(() => {
if (this.onChange && this.formGroup) {
this.onChange(this.transformFromFormGroup(this.formGroup.value));
}
}, 0);
}

this.subscription = this.formGroup.valueChanges
.pipe(
Expand Down

0 comments on commit 0124273

Please sign in to comment.