Skip to content

Commit 4838210

Browse files
Rework validator (#1017)
1 parent 5fa2601 commit 4838210

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

src/core/component.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export abstract class DxComponent implements OnChanges, OnInit, DoCheck, AfterCo
4444
instance: any;
4545
isLinked = true;
4646
changedOptions = {};
47-
createInstanceOnInit = true;
4847
widgetUpdateLocked = false;
4948

5049
private _initTemplates() {
@@ -155,12 +154,8 @@ export abstract class DxComponent implements OnChanges, OnInit, DoCheck, AfterCo
155154
this._initPlatform();
156155
this._initOptions();
157156

158-
let createInstanceOnInit = this.createInstanceOnInit;
159-
160157
this._initialOptions.onInitializing = function () {
161-
if (createInstanceOnInit) {
162-
this.beginUpdate();
163-
}
158+
this.beginUpdate();
164159
};
165160
this.instance = this._createInstance(element, this._initialOptions);
166161
this._initEvents();
@@ -198,9 +193,7 @@ export abstract class DxComponent implements OnChanges, OnInit, DoCheck, AfterCo
198193
}
199194

200195
ngOnInit() {
201-
if (this.createInstanceOnInit) {
202-
this._createWidget(this.element.nativeElement);
203-
}
196+
this._createWidget(this.element.nativeElement);
204197
}
205198

206199
ngDoCheck() {
@@ -214,9 +207,7 @@ export abstract class DxComponent implements OnChanges, OnInit, DoCheck, AfterCo
214207

215208
ngAfterViewInit() {
216209
this._initTemplates();
217-
if (this.createInstanceOnInit) {
218-
this.instance.endUpdate();
219-
}
210+
this.instance.endUpdate();
220211
}
221212

222213
applyOptions() {
@@ -237,8 +228,16 @@ export abstract class DxComponent implements OnChanges, OnInit, DoCheck, AfterCo
237228
}
238229
}
239230

240-
export abstract class DxComponentExtension extends DxComponent {
231+
export abstract class DxComponentExtension extends DxComponent implements OnInit, AfterViewInit {
241232
createInstance(element: any) {
242233
this._createWidget(element);
243234
}
235+
236+
ngOnInit() {
237+
}
238+
239+
ngAfterViewInit() {
240+
this._createWidget(this.element.nativeElement);
241+
this.instance.endUpdate();
242+
}
244243
}

src/core/nested-option.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ export class NestedOptionHost {
216216
private _host: INestedOptionContainer;
217217
private _optionPath: IOptionPathGetter;
218218

219+
getHost(): INestedOptionContainer {
220+
return this._host;
221+
}
222+
219223
setHost(host: INestedOptionContainer, optionPath?: IOptionPathGetter) {
220224
this._host = host;
221225
this._optionPath = optionPath || (() => '');

templates/component.tst

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
<# var implementedInterfaces = ['OnDestroy']; #>
88

9-
<# it.isEditor && implementedInterfaces.push('OnInit') && implementedInterfaces.push('AfterViewInit'); #>
109
<# it.isEditor && implementedInterfaces.push('ControlValueAccessor'); #>
1110
<# collectionProperties.length && implementedInterfaces.push('OnChanges', 'DoCheck'); #>
1211

@@ -23,11 +22,11 @@ import {
2322

2423
Input,
2524
Output,
26-
OnDestroy,
25+
OnDestroy,<#? it.isExtension #>
26+
SkipSelf,
27+
Optional,
28+
Host,<#?#>
2729
EventEmitter<#? it.isEditor #>,
28-
OnInit,
29-
AfterViewInit,
30-
ContentChild,
3130
forwardRef,
3231
HostListener<#?#><#? collectionProperties.length #>,
3332
OnChanges,
@@ -42,8 +41,6 @@ import DevExpress from 'devextreme/bundles/dx.all';<#?#>
4241

4342
import <#= it.className #> from '<#= it.module #>';
4443
<#? it.isEditor #>
45-
import { DxValidatorComponent } from './validator';
46-
4744
import {
4845
ControlValueAccessor,
4946
NG_VALUE_ACCESSOR
@@ -87,10 +84,6 @@ const CUSTOM_VALUE_ACCESSOR_PROVIDER = {
8784
})
8885
export class <#= it.className #>Component extends <#= baseClass #> <#? implementedInterfaces.length #>implements <#= implementedInterfaces.join(', ') #> <#?#>{
8986
instance: <#= it.className #>;
90-
<#? it.isEditor #>
91-
@ContentChild(DxValidatorComponent)
92-
validator: DxValidatorComponent;
93-
<#?#>
9487
<#~ it.properties :prop:i #><#? prop.description #>
9588
/**
9689
* <#= prop.description #>
@@ -125,9 +118,15 @@ export class <#= it.className #>Component extends <#= baseClass #> <#? implement
125118
}
126119
<#~#>
127120

121+
<#? it.isExtension #>
122+
parentElement: any;
123+
<#?#>
124+
128125
constructor(elementRef: ElementRef, ngZone: NgZone, templateHost: DxTemplateHost,
129126
<#? collectionProperties.length #>private <#?#>_watcherHelper: WatcherHelper<#? collectionProperties.length #>,
130-
private _idh: IterableDifferHelper<#?#>, optionHost: NestedOptionHost,
127+
private _idh: IterableDifferHelper<#?#>,<#? it.isExtension #>
128+
@SkipSelf() @Optional() @Host() parentOptionHost: NestedOptionHost,<#?#>
129+
optionHost: NestedOptionHost,
131130
transferState: TransferState,
132131
@Inject(PLATFORM_ID) platformId: any) {
133132

@@ -136,15 +135,30 @@ export class <#= it.className #>Component extends <#= baseClass #> <#? implement
136135
this._createEventEmitters([
137136
<#~ it.events :event:i #>{ <#? event.subscribe #>subscribe: '<#= event.subscribe #>', <#?#>emit: '<#= event.emit #>' }<#? i < it.events.length-1 #>,
138137
<#?#><#~#>
139-
]);<#? collectionProperties.length #>
138+
]);<#? it.isExtension #>
139+
this.parentElement = this.getParentElement(parentOptionHost);<#?#><#? collectionProperties.length #>
140140

141141
this._idh.setHost(this);<#?#>
142142
optionHost.setHost(this);
143143
}
144144

145145
protected _createInstance(element, options) {
146+
<#? it.isExtension #>
147+
if (this.parentElement) {
148+
return new DxValidator(this.parentElement, options);
149+
}
150+
<#?#>
146151
return new <#= it.className #>(element, options);
147152
}
153+
<#? it.isExtension #>
154+
private getParentElement(host) {
155+
if (host) {
156+
const parentHost = host.getHost();
157+
return (parentHost as any).element.nativeElement;
158+
}
159+
return;
160+
}
161+
<#?#>
148162
<#? it.isEditor #>
149163
writeValue(value: any): void {
150164
this.eventHelper.lockedValueChangeEvent = true;
@@ -196,19 +210,6 @@ export class <#= it.className #>Component extends <#= baseClass #> <#? implement
196210
super._setOption(name, value);
197211
}
198212
}<#?#>
199-
<#? it.isEditor #>
200-
ngOnInit() {
201-
super.ngOnInit();
202-
if (this.validator) {
203-
this.validator.createInstanceOnInit = false;
204-
}
205-
}
206-
ngAfterViewInit() {
207-
super.ngAfterViewInit();
208-
if (this.validator) {
209-
this.validator.createInstance(this.element.nativeElement);
210-
}
211-
}<#?#>
212213
}
213214

214215
@NgModule({

0 commit comments

Comments
 (0)