Skip to content

Commit 9f7859e

Browse files
DataHelper: fix errors after migration to TS
1 parent 88d815e commit 9f7859e

File tree

4 files changed

+323
-136
lines changed

4 files changed

+323
-136
lines changed
Lines changed: 155 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
2-
import { DataSource } from '../../common/data/data_source/data_source';
3-
import { extend } from '../../core/utils/extend';
4-
import { normalizeDataSourceOptions } from '../../common/data/data_source/utils';
5-
import DataController from '../ui/collection/m_data_controller';
1+
import { DataSource } from '@js/common/data/data_source/data_source';
2+
import { normalizeDataSourceOptions } from '@js/common/data/data_source/utils';
3+
import { extend } from '@js/core/utils/extend';
4+
import type { Controller } from '@ts/grids/grid_core/m_modules';
5+
import type { ModuleType } from '@ts/grids/grid_core/m_types';
6+
import DataController from '@ts/ui/collection/m_data_controller';
67

78
const DATA_SOURCE_OPTIONS_METHOD = '_dataSourceOptions';
89
const DATA_SOURCE_CHANGED_METHOD = '_dataSourceChangedHandler';
@@ -12,155 +13,175 @@ const DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD = '_dataSourceFromUrlLoadMode';
1213
const SPECIFIC_DATA_SOURCE_OPTION = '_getSpecificDataSourceOption';
1314
const NORMALIZE_DATA_SOURCE = '_normalizeDataSource';
1415

16+
export const DataHelperMixin = <T extends ModuleType<Controller>>(Base: T) => class DataHelperMixin extends Base {
17+
public _dataSource: any;
1518

16-
const DataHelperMixin = {
19+
protected _dataController: any;
1720

18-
postCtor: function() {
19-
this.on('disposing', function() {
20-
this._disposeDataSource();
21-
}.bind(this));
22-
},
21+
protected readyWatcher: any;
2322

24-
_refreshDataSource: function() {
25-
this._initDataSource();
26-
this._loadDataSource();
27-
},
23+
private _proxiedDataSourceChangedHandler: any;
2824

29-
_initDataSource: function() {
30-
let dataSourceOptions = (SPECIFIC_DATA_SOURCE_OPTION in this) ? this[SPECIFIC_DATA_SOURCE_OPTION]() : this.option('dataSource');
31-
let widgetDataSourceOptions;
32-
let dataSourceType;
25+
private _proxiedDataSourceLoadErrorHandler: any;
3326

34-
this._disposeDataSource();
27+
private _proxiedDataSourceLoadingChangedHandler: any;
3528

36-
if(dataSourceOptions) {
37-
if(dataSourceOptions instanceof DataSource) {
38-
this._isSharedDataSource = true;
39-
this._dataSource = dataSourceOptions;
40-
} else {
41-
widgetDataSourceOptions = (DATA_SOURCE_OPTIONS_METHOD in this) ? this[DATA_SOURCE_OPTIONS_METHOD]() : {};
42-
dataSourceType = this._dataSourceType ? this._dataSourceType() : DataSource;
29+
protected _isSharedDataSource: any;
4330

44-
dataSourceOptions = normalizeDataSourceOptions(dataSourceOptions, {
45-
fromUrlLoadMode: (DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD in this) && this[DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD]()
46-
});
31+
private readonly _dataSourceType: any;
4732

48-
this._dataSource = new dataSourceType(extend(true, {}, widgetDataSourceOptions, dataSourceOptions));
49-
}
33+
public postCtor() {
34+
this.on('disposing', () => {
35+
this._disposeDataSource();
36+
});
37+
}
5038

51-
if(NORMALIZE_DATA_SOURCE in this) {
52-
this._dataSource = this[NORMALIZE_DATA_SOURCE](this._dataSource);
53-
}
39+
protected _refreshDataSource() {
40+
this._initDataSource();
41+
this._loadDataSource();
42+
}
5443

55-
this._addDataSourceHandlers();
56-
this._initDataController();
57-
}
58-
},
44+
protected _initDataSource() {
45+
let dataSourceOptions = SPECIFIC_DATA_SOURCE_OPTION in this
46+
? (this[SPECIFIC_DATA_SOURCE_OPTION] as any)()
47+
: this.option('dataSource');
5948

60-
_initDataController: function() {
61-
const dataController = this.option?.('_dataController');
62-
const dataSource = this._dataSource;
49+
let widgetDataSourceOptions;
50+
let dataSourceType;
6351

64-
if(dataController) {
65-
this._dataController = dataController;
66-
} else {
67-
this._dataController = new DataController(dataSource);
68-
}
69-
},
52+
this._disposeDataSource();
7053

71-
_addDataSourceHandlers: function() {
72-
if(DATA_SOURCE_CHANGED_METHOD in this) {
73-
this._addDataSourceChangeHandler();
74-
}
54+
if (dataSourceOptions) {
55+
if (dataSourceOptions instanceof DataSource) {
56+
this._isSharedDataSource = true;
57+
this._dataSource = dataSourceOptions;
58+
} else {
59+
widgetDataSourceOptions = DATA_SOURCE_OPTIONS_METHOD in this
60+
? (this[DATA_SOURCE_OPTIONS_METHOD] as any)()
61+
: {};
7562

76-
if(DATA_SOURCE_LOAD_ERROR_METHOD in this) {
77-
this._addDataSourceLoadErrorHandler();
78-
}
63+
dataSourceType = this._dataSourceType ? this._dataSourceType() : DataSource;
7964

80-
if(DATA_SOURCE_LOADING_CHANGED_METHOD in this) {
81-
this._addDataSourceLoadingChangedHandler();
82-
}
65+
dataSourceOptions = normalizeDataSourceOptions(dataSourceOptions, {
66+
fromUrlLoadMode: (DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD in this) && (this[DATA_SOURCE_FROM_URL_LOAD_MODE_METHOD] as any)(),
67+
});
8368

84-
this._addReadyWatcher();
85-
},
86-
87-
_addReadyWatcher: function() {
88-
this.readyWatcher = (function(isLoading) {
89-
this._ready && this._ready(!isLoading);
90-
}).bind(this);
91-
this._dataSource.on('loadingChanged', this.readyWatcher);
92-
},
93-
94-
_addDataSourceChangeHandler: function() {
95-
const dataSource = this._dataSource;
96-
this._proxiedDataSourceChangedHandler = (function(e) {
97-
this[DATA_SOURCE_CHANGED_METHOD](dataSource.items(), e);
98-
}).bind(this);
99-
dataSource.on('changed', this._proxiedDataSourceChangedHandler);
100-
},
101-
102-
_addDataSourceLoadErrorHandler: function() {
103-
this._proxiedDataSourceLoadErrorHandler = this[DATA_SOURCE_LOAD_ERROR_METHOD].bind(this);
104-
this._dataSource.on('loadError', this._proxiedDataSourceLoadErrorHandler);
105-
},
106-
107-
_addDataSourceLoadingChangedHandler: function() {
108-
this._proxiedDataSourceLoadingChangedHandler = this[DATA_SOURCE_LOADING_CHANGED_METHOD].bind(this);
109-
this._dataSource.on('loadingChanged', this._proxiedDataSourceLoadingChangedHandler);
110-
},
111-
112-
_loadDataSource: function() {
113-
const dataSource = this._dataSource;
114-
if(dataSource) {
115-
if(dataSource.isLoaded()) {
116-
this._proxiedDataSourceChangedHandler && this._proxiedDataSourceChangedHandler();
117-
} else {
118-
dataSource.load();
119-
}
120-
}
121-
},
122-
123-
_loadSingle: function(key, value) {
124-
key = key === 'this' ? this._dataSource.key() || 'this' : key;
125-
return this._dataSource.loadSingle(key, value);
126-
},
127-
128-
_isLastPage: function() {
129-
return !this._dataSource || this._dataSource.isLastPage() || !this._dataSource._pageSize;
130-
},
131-
132-
_isDataSourceLoading: function() {
133-
return this._dataSource && this._dataSource.isLoading();
134-
},
135-
136-
_disposeDataSource: function() {
137-
if(this._dataSource) {
138-
if(this._isSharedDataSource) {
139-
delete this._isSharedDataSource;
140-
141-
this._proxiedDataSourceChangedHandler && this._dataSource.off('changed', this._proxiedDataSourceChangedHandler);
142-
this._proxiedDataSourceLoadErrorHandler && this._dataSource.off('loadError', this._proxiedDataSourceLoadErrorHandler);
143-
this._proxiedDataSourceLoadingChangedHandler && this._dataSource.off('loadingChanged', this._proxiedDataSourceLoadingChangedHandler);
144-
145-
if(this._dataSource._eventsStrategy) {
146-
this._dataSource._eventsStrategy.off('loadingChanged', this.readyWatcher);
147-
}
148-
} else {
149-
this._dataSource.dispose();
150-
}
151-
152-
delete this._dataSource;
153-
154-
delete this._proxiedDataSourceChangedHandler;
155-
delete this._proxiedDataSourceLoadErrorHandler;
156-
delete this._proxiedDataSourceLoadingChangedHandler;
69+
// eslint-disable-next-line new-cap
70+
this._dataSource = new dataSourceType(extend(true, {}, widgetDataSourceOptions, dataSourceOptions));
71+
}
72+
73+
if (NORMALIZE_DATA_SOURCE in this) {
74+
this._dataSource = (this[NORMALIZE_DATA_SOURCE] as any)(this._dataSource);
75+
}
76+
77+
this._addDataSourceHandlers();
78+
this._initDataController();
79+
}
80+
}
81+
82+
private _initDataController() {
83+
const dataController = this.option?.('_dataController');
84+
const dataSource = this._dataSource;
85+
86+
if (dataController) {
87+
this._dataController = dataController;
88+
} else {
89+
this._dataController = new DataController(dataSource);
90+
}
91+
}
92+
93+
private _addDataSourceHandlers() {
94+
if (DATA_SOURCE_CHANGED_METHOD in this) {
95+
this._addDataSourceChangeHandler();
96+
}
97+
98+
if (DATA_SOURCE_LOAD_ERROR_METHOD in this) {
99+
this._addDataSourceLoadErrorHandler();
100+
}
101+
102+
if (DATA_SOURCE_LOADING_CHANGED_METHOD in this) {
103+
this._addDataSourceLoadingChangedHandler();
104+
}
105+
106+
this._addReadyWatcher();
107+
}
108+
109+
private _addReadyWatcher() {
110+
this.readyWatcher = function (isLoading) {
111+
this._ready && this._ready(!isLoading);
112+
}.bind(this);
113+
this._dataSource.on('loadingChanged', this.readyWatcher);
114+
}
115+
116+
private _addDataSourceChangeHandler() {
117+
const dataSource = this._dataSource;
118+
this._proxiedDataSourceChangedHandler = function (e) {
119+
this[DATA_SOURCE_CHANGED_METHOD](dataSource.items(), e);
120+
}.bind(this);
121+
dataSource.on('changed', this._proxiedDataSourceChangedHandler);
122+
}
123+
124+
private _addDataSourceLoadErrorHandler() {
125+
this._proxiedDataSourceLoadErrorHandler = this[DATA_SOURCE_LOAD_ERROR_METHOD].bind(this);
126+
this._dataSource.on('loadError', this._proxiedDataSourceLoadErrorHandler);
127+
}
128+
129+
private _addDataSourceLoadingChangedHandler() {
130+
this._proxiedDataSourceLoadingChangedHandler = this[DATA_SOURCE_LOADING_CHANGED_METHOD].bind(this);
131+
this._dataSource.on('loadingChanged', this._proxiedDataSourceLoadingChangedHandler);
132+
}
133+
134+
protected _loadDataSource() {
135+
const dataSource = this._dataSource;
136+
if (dataSource) {
137+
if (dataSource.isLoaded()) {
138+
this._proxiedDataSourceChangedHandler && this._proxiedDataSourceChangedHandler();
139+
} else {
140+
dataSource.load();
141+
}
142+
}
143+
}
144+
145+
private _loadSingle(key, value) {
146+
key = key === 'this' ? this._dataSource.key() || 'this' : key;
147+
return this._dataSource.loadSingle(key, value);
148+
}
149+
150+
private _isLastPage() {
151+
return !this._dataSource || this._dataSource.isLastPage() || !this._dataSource._pageSize;
152+
}
153+
154+
private _isDataSourceLoading() {
155+
return this._dataSource && this._dataSource.isLoading();
156+
}
157+
158+
protected _disposeDataSource() {
159+
if (this._dataSource) {
160+
if (this._isSharedDataSource) {
161+
delete this._isSharedDataSource;
162+
163+
this._proxiedDataSourceChangedHandler && this._dataSource.off('changed', this._proxiedDataSourceChangedHandler);
164+
this._proxiedDataSourceLoadErrorHandler && this._dataSource.off('loadError', this._proxiedDataSourceLoadErrorHandler);
165+
this._proxiedDataSourceLoadingChangedHandler && this._dataSource.off('loadingChanged', this._proxiedDataSourceLoadingChangedHandler);
166+
167+
if (this._dataSource._eventsStrategy) {
168+
this._dataSource._eventsStrategy.off('loadingChanged', this.readyWatcher);
157169
}
158-
},
170+
} else {
171+
this._dataSource.dispose();
172+
}
173+
174+
delete this._dataSource;
159175

160-
getDataSource: function() {
161-
return this._dataSource || null;
176+
delete this._proxiedDataSourceChangedHandler;
177+
delete this._proxiedDataSourceLoadErrorHandler;
178+
delete this._proxiedDataSourceLoadingChangedHandler;
162179
}
180+
}
163181

182+
protected getDataSource() {
183+
return this._dataSource || null;
184+
}
164185
};
165186

166187
export default DataHelperMixin;

packages/devextreme/js/__internal/ui/collection/collection_widget.base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { getOuterHeight, getOuterWidth } from '@js/core/utils/size';
2626
import { findTemplates } from '@js/core/utils/template_manager';
2727
import { isDefined, isFunction, isPlainObject } from '@js/core/utils/type';
2828
import type { DataSourceOptions } from '@js/data/data_source';
29+
import DataHelperMixin from '@js/data_helper';
2930
import type {
3031
Cancelable, DxEvent, EventInfo, ItemInfo,
3132
} from '@js/events';
@@ -34,7 +35,6 @@ import { focusable } from '@js/ui/widget/selectors';
3435
import { getPublicElement } from '@ts/core/m_element';
3536
import type { OptionChanged } from '@ts/core/widget/types';
3637
import Widget from '@ts/core/widget/widget';
37-
import DataHelperMixin from '@ts/data/m_data_helper';
3838
import CollectionWidgetItem from '@ts/ui/collection/m_item';
3939

4040
const COLLECTION_CLASS = 'dx-collection';

packages/devextreme/js/common/data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { errorHandler, setErrorHandler } from './data/errors';
77
import LocalStore from './data/local_store';
88
import query from './data/query';
99
import { base64_encode, compileGetter, compileSetter } from './data/utils';
10-
import DataHelperMixin from '../__internal/data/m_data_helper';
10+
import DataHelperMixin from './data/data_helper';
1111
import ODataContext from './data/odata/context';
1212
import ODataStore from './data/odata/store';
1313
import { EdmLiteral, keyConverters } from './data/odata/utils';

0 commit comments

Comments
 (0)