@kaspernj/api-maker 1.0.280 → 1.0.282
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/base-model.mjs +20 -7
package/package.json
CHANGED
package/src/base-model.mjs
CHANGED
|
@@ -17,6 +17,18 @@ import Services from "./services.mjs"
|
|
|
17
17
|
import ValidationError from "./validation-error.mjs"
|
|
18
18
|
import {ValidationErrors} from "./validation-errors.mjs"
|
|
19
19
|
|
|
20
|
+
const objectToUnderscore = (object) => {
|
|
21
|
+
const newObject = {}
|
|
22
|
+
|
|
23
|
+
for (const key in object) {
|
|
24
|
+
const underscoreKey = inflection.underscore(key)
|
|
25
|
+
|
|
26
|
+
newObject[underscoreKey] = object[key]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return newObject
|
|
30
|
+
}
|
|
31
|
+
|
|
20
32
|
export default class BaseModel {
|
|
21
33
|
static apiMakerType = "BaseModel"
|
|
22
34
|
|
|
@@ -171,10 +183,10 @@ export default class BaseModel {
|
|
|
171
183
|
this._readModelDataFromArgs(args)
|
|
172
184
|
} else if (args.a) {
|
|
173
185
|
this.abilities = args.b || {}
|
|
174
|
-
this.modelData = args.a
|
|
186
|
+
this.modelData = objectToUnderscore(args.a)
|
|
175
187
|
} else if (args) {
|
|
176
188
|
this.abilities = {}
|
|
177
|
-
this.modelData = args
|
|
189
|
+
this.modelData = objectToUnderscore(args)
|
|
178
190
|
} else {
|
|
179
191
|
this.abilities = {}
|
|
180
192
|
this.modelData = {}
|
|
@@ -184,13 +196,14 @@ export default class BaseModel {
|
|
|
184
196
|
assignAttributes (newAttributes) {
|
|
185
197
|
for (const key in newAttributes) {
|
|
186
198
|
const newValue = newAttributes[key]
|
|
199
|
+
const attributeUnderscore = inflection.underscore(key)
|
|
187
200
|
|
|
188
201
|
let applyChange = true
|
|
189
202
|
let deleteChange = false
|
|
190
203
|
|
|
191
204
|
if (this.isAttributeLoaded(key)) {
|
|
192
|
-
const oldValue = this.
|
|
193
|
-
const originalValue = this.modelData[
|
|
205
|
+
const oldValue = this.readAttribute(key)
|
|
206
|
+
const originalValue = this.modelData[attributeUnderscore]
|
|
194
207
|
|
|
195
208
|
if (newValue == oldValue) {
|
|
196
209
|
applyChange = false
|
|
@@ -201,9 +214,9 @@ export default class BaseModel {
|
|
|
201
214
|
}
|
|
202
215
|
|
|
203
216
|
if (applyChange) {
|
|
204
|
-
this.changes[
|
|
217
|
+
this.changes[attributeUnderscore] = newValue
|
|
205
218
|
} else if (deleteChange) {
|
|
206
|
-
delete this.changes[
|
|
219
|
+
delete this.changes[attributeUnderscore]
|
|
207
220
|
}
|
|
208
221
|
}
|
|
209
222
|
}
|
|
@@ -907,7 +920,7 @@ export default class BaseModel {
|
|
|
907
920
|
_readModelDataFromArgs (args) {
|
|
908
921
|
this.abilities = args.data.b || {}
|
|
909
922
|
this.collection = args.collection
|
|
910
|
-
this.modelData = args.data.a
|
|
923
|
+
this.modelData = objectToUnderscore(args.data.a)
|
|
911
924
|
this.preloadedRelationships = args.data.r
|
|
912
925
|
}
|
|
913
926
|
|