Skip to content

Commit 2a54986

Browse files
committed
rework the strategy of applying options
1 parent 8ee05b2 commit 2a54986

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

src/core/component.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {
22
ElementRef,
33
NgZone,
44
QueryList,
5-
AfterViewInit
5+
AfterViewInit,
6+
AfterContentChecked
67
} from '@angular/core';
78

89
import { DxTemplateDirective } from './template';
@@ -16,8 +17,9 @@ import {
1617
CollectionNestedOptionContainerImpl
1718
} from './nested-option';
1819

19-
export abstract class DxComponent implements AfterViewInit, INestedOptionContainer, ICollectionNestedOptionContainer {
20+
export abstract class DxComponent implements AfterViewInit, AfterContentChecked, INestedOptionContainer, ICollectionNestedOptionContainer {
2021
private _initialOptions: any;
22+
private _optionToUpdate: any;
2123
private _collectionContainerImpl: ICollectionNestedOptionContainer;
2224
eventHelper: EmitterHelper;
2325
templates: DxTemplateDirective[];
@@ -63,14 +65,14 @@ export abstract class DxComponent implements AfterViewInit, INestedOptionContain
6365
}
6466
protected _setOption(name: string, value: any) {
6567
if (this.instance) {
66-
this._updateOption(name, value);
68+
this._prepareOptionToUpdate(name, value);
6769
} else {
6870
this._initialOptions[name] = value;
6971
}
7072
}
71-
protected _updateOption(name: string, value: any) {
73+
protected _prepareOptionToUpdate(name: string, value: any) {
7274
if (this._shouldOptionChange(name, value)) {
73-
this.instance.option(name, value);
75+
this._optionToUpdate[name] = value;
7476
};
7577
}
7678
protected abstract _createInstance(element, options)
@@ -114,11 +116,18 @@ export abstract class DxComponent implements AfterViewInit, INestedOptionContain
114116
}
115117
constructor(protected element: ElementRef, private ngZone: NgZone, templateHost: DxTemplateHost, private watcherHelper: WatcherHelper) {
116118
this._initialOptions = { integrationOptions: {} };
119+
this._optionToUpdate = {};
117120
this.templates = [];
118121
templateHost.setHost(this);
119122
this._collectionContainerImpl = new CollectionNestedOptionContainerImpl(this._setOption.bind(this));
120123
this.eventHelper = new EmitterHelper(this.ngZone, this);
121124
}
125+
ngAfterContentChecked() {
126+
if (this.instance && Object.keys(this._optionToUpdate).length) {
127+
this.instance.option(this._optionToUpdate);
128+
this._optionToUpdate = {};
129+
}
130+
}
122131
ngAfterViewInit() {
123132
if (this.renderOnViewInit) {
124133
this._createWidget(this.element.nativeElement);

templates/component.tst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ export class <#= it.className #>Component extends <#= baseClass #> <#? implement
148148
this._watcherHelper.checkWatchers();
149149
}
150150

151-
_updateOption(name: string, value: any) {
151+
_prepareOptionToUpdate(name: string, value: any) {
152152
if (Array.isArray(value)) {
153153
this._idh.setupSingle(name, value);
154154
this._idh.getChanges(name, value);
155155
}
156156

157-
super._updateOption(name, value);
157+
super._prepareOptionToUpdate(name, value);
158158
}<#?#>
159159
<#? it.isEditor #>
160160
ngAfterContentInit() {

tests/src/core/component.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ describe('DevExtreme Angular widget', () => {
229229
instance = getWidget(fixture);
230230

231231
testComponent.testOption = 'Changed 2';
232+
fixture.detectChanges();
232233
expect(instance.option('testOption')).toBe('Changed 2');
233234

234235
}));

tests/src/ui/tab-panel.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* tslint:disable:component-selector */
2+
3+
import {
4+
Component,
5+
ViewChild
6+
} from '@angular/core';
7+
8+
import {
9+
TestBed,
10+
async
11+
} from '@angular/core/testing';
12+
13+
import {
14+
DxTabPanelModule,
15+
DxTabPanelComponent
16+
} from '../../../dist';
17+
18+
@Component({
19+
selector: 'test-container-component',
20+
template: `
21+
<dx-tab-panel
22+
[dataSource]="tabPanelItems"
23+
[selectedIndex]="selectedIndex">
24+
</dx-tab-panel>
25+
`
26+
})
27+
class TestContainerComponent {
28+
@ViewChild(DxTabPanelComponent) tabPanel: DxTabPanelComponent;
29+
30+
tabPanelItems: number[] = [0];
31+
selectedIndex = 0;
32+
}
33+
34+
describe('DxTagBox', () => {
35+
36+
beforeEach(() => {
37+
TestBed.configureTestingModule(
38+
{
39+
declarations: [TestContainerComponent],
40+
imports: [DxTabPanelModule]
41+
});
42+
});
43+
44+
// spec
45+
it('option, dependenced on dataSource, slould be applyed', async(() => {
46+
let fixture = TestBed.createComponent(TestContainerComponent);
47+
fixture.detectChanges();
48+
49+
let component: any = fixture.componentInstance;
50+
component.tabPanelItems.push(1);
51+
component.selectedIndex = 1;
52+
fixture.detectChanges();
53+
54+
let instance: any = component.tabPanel.instance;
55+
expect(instance.option('items').length).toBe(2);
56+
expect(instance.option('selectedIndex')).toBe(1);
57+
}));
58+
});

0 commit comments

Comments
 (0)