Skip to content

Commit cc7a558

Browse files
committed
fix(core): Use OnChanges instead of OnInit
This lets us throw a better error and ultimately prepares for future changes where we support changing values at runtime. fixes #52
1 parent 60d8bd8 commit cc7a558

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

projects/ngqp/core/src/lib/directives/query-param-group.directive.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Directive, Input, OnInit } from '@angular/core';
1+
import { Directive, Input, OnChanges, SimpleChanges } from '@angular/core';
22
import { QueryParamNameDirective } from './query-param-name.directive';
33
import { QueryParamGroup } from '../model/query-param-group';
44
import { QueryParamGroupService } from './query-param-group.service';
@@ -14,7 +14,7 @@ import { QueryParamGroupService } from './query-param-group.service';
1414
selector: '[queryParamGroup]',
1515
providers: [QueryParamGroupService],
1616
})
17-
export class QueryParamGroupDirective implements OnInit {
17+
export class QueryParamGroupDirective implements OnChanges {
1818

1919
/**
2020
* The {@link QueryParamGroup} to bind.
@@ -26,12 +26,20 @@ export class QueryParamGroupDirective implements OnInit {
2626
}
2727

2828
/** @ignore */
29-
public ngOnInit() {
30-
if (!this.queryParamGroup) {
31-
throw new Error(`You added the queryParamGroup directive, but haven't supplied a group to use.`);
32-
}
29+
public ngOnChanges(changes: SimpleChanges) {
30+
const groupChange = changes['queryParamGroup'];
31+
if (groupChange) {
32+
if (!groupChange.firstChange) {
33+
throw new Error(`Binding a different QueryParamGroup during runtime is currently not supported.`);
34+
}
35+
36+
const queryParamGroup = groupChange.currentValue;
37+
if (!queryParamGroup) {
38+
throw new Error(`You added the queryParamGroup directive, but haven't supplied a group to use.`);
39+
}
3340

34-
this.groupService.setQueryParamGroup(this.queryParamGroup);
41+
this.groupService.setQueryParamGroup(queryParamGroup);
42+
}
3543
}
3644

3745
}

projects/ngqp/core/src/lib/directives/query-param-name.directive.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Directive, Inject, Input, OnInit, Optional, Self } from '@angular/core';
1+
import { Directive, Inject, Input, OnChanges, Optional, Self, SimpleChanges } from '@angular/core';
22
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
33
import { DefaultControlValueAccessorDirective, NGQP_BUILT_IN_ACCESSORS } from '../accessors/accessors';
44
import { QueryParamGroupService } from './query-param-group.service';
@@ -13,7 +13,7 @@ import { QueryParamGroupService } from './query-param-group.service';
1313
@Directive({
1414
selector: '[queryParamName]',
1515
})
16-
export class QueryParamNameDirective implements OnInit {
16+
export class QueryParamNameDirective implements OnChanges {
1717

1818
/**
1919
* The name of the {@link QueryParam} inside its parent {@link QueryParamGroup}.
@@ -37,12 +37,20 @@ export class QueryParamNameDirective implements OnInit {
3737
}
3838

3939
/** @ignore */
40-
public ngOnInit() {
41-
if (!this.name) {
42-
throw new Error(`queryParamName has been added, but without specifying the name.`);
43-
}
40+
public ngOnChanges(changes: SimpleChanges) {
41+
const nameChange = changes['name'];
42+
if (nameChange) {
43+
if (!nameChange.firstChange) {
44+
throw new Error(`You tried to switch from queryParamName=${nameChange.previousValue} to queryParamName=${nameChange.currentValue} which is currently not supported.`);
45+
}
4446

45-
this.groupService.addQueryParam(this);
47+
const name = nameChange.currentValue;
48+
if (!name) {
49+
throw new Error(`queryParamName has been added, but without specifying the name.`);
50+
}
51+
52+
this.groupService.addQueryParam(this);
53+
}
4654
}
4755

4856
/**

0 commit comments

Comments
 (0)