Skip to content

Commit 0581fdc

Browse files
authored
Fix issue with sjsOptions not being scoped correctly (#37)
1 parent 1183eb2 commit 0581fdc

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

__tests__/data/models/UserResponseModel.ts

-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,4 @@ export class UserResponseModel extends BaseModel {
1616

1717
this.update(data);
1818
}
19-
20-
public update(data: Partial<UserResponseModel>): void {
21-
super.update(data);
22-
}
2319
}

src/BaseModel.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import { Util } from './BaseModel.utils';
33
import { ConversionTypeEnum } from './BaseModel.constants';
44
import { BaseModelJson, IBaseModel, IBaseModelOptions, IConversionOption } from './BaseModel.types';
55

6-
const sjsOptions: IBaseModelOptions = {
7-
expand: false,
8-
};
9-
106
/**
117
* BaseModel is a design pattern used to transfer data between software application subsystems.
128
*
@@ -71,10 +67,14 @@ export class BaseModel extends BaseObject implements IBaseModel {
7167
*/
7268
public static readonly IS_BASE_MODEL: boolean = true;
7369

70+
#sjsOptions: IBaseModelOptions = {
71+
expand: false,
72+
};
73+
7474
constructor(opts: IBaseModelOptions = {}) {
7575
super();
7676

77-
sjsOptions.expand = opts.expand === true;
77+
this.#sjsOptions.expand = opts.expand === true;
7878
}
7979

8080
/**
@@ -178,11 +178,11 @@ export class BaseModel extends BaseObject implements IBaseModel {
178178
Array.isArray(passedInDataForProperty) === false ? [passedInDataForProperty] : passedInDataForProperty;
179179

180180
if (isBaseModelClass) {
181-
return arrayData.map((json: object) => new fistItemInArray(json, sjsOptions));
181+
return arrayData.map((json: object) => new fistItemInArray(json, this.#sjsOptions));
182182
}
183183

184184
if (isBaseModelObject) {
185-
return arrayData.map((json: object) => new (fistItemInArray as any).constructor(json, sjsOptions));
185+
return arrayData.map((json: object) => new (fistItemInArray as any).constructor(json, this.#sjsOptions));
186186
}
187187

188188
return arrayData;
@@ -207,12 +207,12 @@ export class BaseModel extends BaseObject implements IBaseModel {
207207
return baseModel;
208208
}
209209

210-
if (isBaseModelClass && (isPassedInDataAnObjectWithProperties || sjsOptions.expand === true)) {
210+
if (isBaseModelClass && (isPassedInDataAnObjectWithProperties || this.#sjsOptions.expand === true)) {
211211
// If data is passed in or the expand option is set to true then create the BaseModel.
212212
// Give the constructor the passed in data or an empty object if expand is true.
213213
const obj: object = isPassedInDataAnObjectWithProperties ? passedInDataForProperty : {};
214214

215-
return new currentPropertyData(obj, sjsOptions);
215+
return new currentPropertyData(obj, this.#sjsOptions);
216216
} else if (isBaseModelClass) {
217217
// Don't create the BaseModel if there is no data passed in. Return null to be assigned to the property.
218218
return null;

0 commit comments

Comments
 (0)