Skip to content

Commit b5084bb

Browse files
committed
- add an option to disable getting DataTable classes from the global scope
1 parent c931e8a commit b5084bb

File tree

5 files changed

+115
-9
lines changed

5 files changed

+115
-9
lines changed

backendless.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
declare module Backendless {
66
let debugMode: boolean;
7+
let useTableClassesFromGlobalScope: boolean;
78
let serverURL: string;
89
let appId: string;
910
let apiKey: string;
@@ -35,7 +36,8 @@ declare module Backendless {
3536
serverURL?: string;
3637
domain?: string;
3738
debugMode?: boolean;
38-
XMLHttpRequest?: XMLHttpRequest,
39+
XMLHttpRequest?: XMLHttpRequest;
40+
useTableClassesFromGlobalScope?: boolean;
3941
}
4042

4143
/**

src/data/store.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,14 @@ export default class DataStore {
312312
* @private
313313
* */
314314
parseRelationsResponse(result, RelationModel) {
315-
return convertToClientRecords(result, RelationModel, this.classToTableMap)
315+
return convertToClientRecords(result, RelationModel, this)
316316
}
317317

318318
/**
319319
* @private
320320
* */
321321
parseResponse(result) {
322-
return convertToClientRecords(result, this.model, this.classToTableMap)
322+
return convertToClientRecords(result, this.model, this)
323323
}
324324

325325
/**
@@ -419,16 +419,17 @@ const convertToServerRecord = (() => {
419419
})()
420420

421421
const convertToClientRecords = (() => {
422-
return (records, RootModel, classToTableMap) => {
422+
return (records, RootModel, dataStore) => {
423423
if (!records) {
424424
return records
425425
}
426426

427427
const context = {
428428
RootModel,
429-
classToTableMap,
430-
subIds : {},
431-
postAssign: [],
429+
app : dataStore.app,
430+
classToTableMap: dataStore.classToTableMap,
431+
subIds : {},
432+
postAssign : [],
432433
}
433434

434435
const result = Array.isArray(records)
@@ -448,7 +449,19 @@ const convertToClientRecords = (() => {
448449
delete source[prop].__subID
449450

450451
} else {
451-
const Model = context.classToTableMap[source[prop].___class] || Utils.globalScope[source[prop].___class]
452+
let Model = context.classToTableMap[source[prop].___class]
453+
454+
if (!Model && context.app.useTableClassesFromGlobalScope && Utils.globalScope[source[prop].___class]) {
455+
// eslint-disable-next-line no-console
456+
console.warn(
457+
'Resolving DataTable classes from the global scope is deprecated ' +
458+
'and it won\'t be supported in the nearest future. ' +
459+
'Instead, you should register your DataTable classes ' +
460+
'using the following method Backendless.Data.mapTableToClass'
461+
)
462+
463+
Model = Utils.globalScope[source[prop].___class]
464+
}
452465

453466
target[prop] = Model ? new Model() : {}
454467

src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const DEFAULT_PROPS = {
1515
XMLHttpRequest: typeof XMLHttpRequest !== 'undefined'
1616
? XMLHttpRequest
1717
: undefined,
18+
19+
//TODO: this is a temporary to disable getting DataTable classes from the global scope
20+
//TODO: it will be removed in the nearest future
21+
useTableClassesFromGlobalScope: true,
1822
}
1923

2024
const STATELESS_PROPS = ['appId', 'apiKey', 'domain']
@@ -255,6 +259,15 @@ class Backendless {
255259
)
256260
}
257261

262+
///--------useTableClassesFromGlobalScope-------///
263+
get useTableClassesFromGlobalScope() {
264+
return this.__useTableClassesFromGlobalScope
265+
}
266+
267+
set useTableClassesFromGlobalScope(useTableClassesFromGlobalScope) {
268+
this.__useTableClassesFromGlobalScope = !!useTableClassesFromGlobalScope
269+
}
270+
258271
///--------debugMode-------///
259272
get debugMode() {
260273
return this.__debugMode

test/tsd.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function testMain() {
2323
apiKey: 'JS_SECRET_KEY',
2424
standalone: true,
2525
debugMode: true,
26+
useTableClassesFromGlobalScope: false,
2627
serverURL: 'serverURL'
2728
});
2829

test/unit/specs/data/parser.js

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2794,7 +2794,13 @@ describe('<Data> Parser', function() {
27942794
expect(result2[1]).to.be.instanceof(FooClass)
27952795
})
27962796

2797-
it('data store object with inner instances from global', async () => {
2797+
it('uses classes from global by default for data store object with inner instances', async () => {
2798+
// eslint-disable-next-line no-console
2799+
const _nativeConsoleWarn = console.warn
2800+
2801+
// eslint-disable-next-line no-console
2802+
const spyConsoleWarn = console.warn = chai.spy()
2803+
27982804
global.FooClass = class FooClass {
27992805
}
28002806

@@ -2831,6 +2837,77 @@ describe('<Data> Parser', function() {
28312837

28322838
expect(result2[1].foo).to.be.instanceof(FooClass)
28332839
expect(result2[1].bar).to.be.instanceof(BarFun)
2840+
2841+
expect(spyConsoleWarn).to.have.been.called.exactly(6)
2842+
2843+
const warningMsg = (
2844+
'Resolving DataTable classes from the global scope is deprecated ' +
2845+
'and it won\'t be supported in the nearest future. ' +
2846+
'Instead, you should register your DataTable classes ' +
2847+
'using the following method Backendless.Data.mapTableToClass'
2848+
)
2849+
2850+
expect(spyConsoleWarn).on.nth(1).be.called.with(warningMsg)
2851+
expect(spyConsoleWarn).on.nth(2).be.called.with(warningMsg)
2852+
expect(spyConsoleWarn).on.nth(3).be.called.with(warningMsg)
2853+
expect(spyConsoleWarn).on.nth(4).be.called.with(warningMsg)
2854+
expect(spyConsoleWarn).on.nth(5).be.called.with(warningMsg)
2855+
expect(spyConsoleWarn).on.nth(6).be.called.with(warningMsg)
2856+
2857+
// eslint-disable-next-line no-console
2858+
console.warn = _nativeConsoleWarn
2859+
})
2860+
2861+
it('does not use classes from global for data store object with inner instances', async () => {
2862+
// eslint-disable-next-line no-console
2863+
const _nativeConsoleWarn = console.warn
2864+
2865+
// eslint-disable-next-line no-console
2866+
const spyConsoleWarn = console.warn = chai.spy()
2867+
2868+
Backendless.useTableClassesFromGlobalScope = false
2869+
2870+
global.FooClass = class FooClass {
2871+
}
2872+
2873+
global.BarFun = function BarFun() {
2874+
}
2875+
2876+
dataStore = Backendless.Data.of(tableName)
2877+
2878+
const result1 = dataStore.parseResponse({
2879+
foo: { ___class: 'FooClass', value: 1, },
2880+
bar: { ___class: 'BarFun', value: 2, }
2881+
})
2882+
2883+
const result2 = dataStore.parseResponse([
2884+
{ foo: { ___class: 'FooClass', value: 3, }, bar: { ___class: 'BarFun', value: 5, } },
2885+
{ foo: { ___class: 'FooClass', value: 4, }, bar: { ___class: 'BarFun', value: 6, } }
2886+
])
2887+
2888+
expect(result1).to.be.eql({
2889+
foo: { ___class: 'FooClass', value: 1, },
2890+
bar: { ___class: 'BarFun', value: 2, }
2891+
})
2892+
2893+
expect(result1.foo).to.not.be.instanceof(FooClass)
2894+
expect(result1.bar).to.not.be.instanceof(BarFun)
2895+
2896+
expect(result2).to.be.eql([
2897+
{ foo: { ___class: 'FooClass', value: 3, }, bar: { ___class: 'BarFun', value: 5, } },
2898+
{ foo: { ___class: 'FooClass', value: 4, }, bar: { ___class: 'BarFun', value: 6, } }
2899+
])
2900+
2901+
expect(result2[0].foo).to.not.be.instanceof(FooClass)
2902+
expect(result2[0].bar).to.not.be.instanceof(BarFun)
2903+
2904+
expect(result2[1].foo).to.not.be.instanceof(FooClass)
2905+
expect(result2[1].bar).to.not.be.instanceof(BarFun)
2906+
2907+
expect(spyConsoleWarn).to.have.been.called.exactly(0)
2908+
2909+
// eslint-disable-next-line no-console
2910+
console.warn = _nativeConsoleWarn
28342911
})
28352912

28362913
it('data store object with inner instances from classesMap', async () => {

0 commit comments

Comments
 (0)