Skip to content

Commit bd61df1

Browse files
committed
feat(model): Added wrappedBy and nowrap methods to turn off / change the data wrapper for the request
1 parent 82c7750 commit bd61df1

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/Model.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export default class Model extends StaticModel {
1212

1313
if (attributes.length === 0) {
1414
this._builder = new Builder(this)
15+
// Set the default data wrapper
16+
this._wrapper = this.wrap()
1517
} else {
1618
Object.assign(this, ...attributes)
1719
this._applyRelations(this)
@@ -71,9 +73,8 @@ export default class Model extends StaticModel {
7173
* @return {object|array} The unwraped response
7274
*/
7375
_unwrap(response) {
74-
const wrapper = this.wrap()
75-
if (wrapper) {
76-
return response[wrapper] || response
76+
if (this._wrapper) {
77+
return response[this._wrapper] || response
7778
} else {
7879
return response
7980
}
@@ -285,6 +286,26 @@ export default class Model extends StaticModel {
285286
return this
286287
}
287288

289+
/**
290+
* The "data" wrapper that will override the wrap() method
291+
*
292+
* @param {string} wrap The new wrapper for this one request
293+
*
294+
* @return {string|null}
295+
*/
296+
wrappedBy(wrap) {
297+
this._wrapper = wrap
298+
return this
299+
}
300+
301+
/**
302+
* Disable wrapping for this one request
303+
*/
304+
nowrap() {
305+
this._wrapper = null
306+
return this
307+
}
308+
288309
/**
289310
* Result
290311
*/

src/StaticModel.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ export default class StaticModel {
8888
return self
8989
}
9090

91+
static wrappedBy(value) {
92+
let self = this.instance()
93+
self.wrappedBy(value)
94+
95+
return self
96+
}
97+
98+
static nowrap() {
99+
let self = this.instance()
100+
self.nowrap()
101+
102+
return self
103+
}
104+
91105
static custom(...args) {
92106
let self = this.instance()
93107
self.custom(...args)

0 commit comments

Comments
 (0)