@onehat/data 1.4.9 → 1.4.10
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.
|
@@ -30,6 +30,43 @@ describe('OneBuildRepository', function() {
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
describe('Params', function() {
|
|
33
|
+
|
|
34
|
+
it('setParam', function() {
|
|
35
|
+
const r = this.repository;
|
|
36
|
+
r.setParam('test', 1);
|
|
37
|
+
|
|
38
|
+
expect(r._params.test).to.be.eq(1);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('setParams', function() {
|
|
42
|
+
const r = this.repository;
|
|
43
|
+
r.setParams({
|
|
44
|
+
test1: 1,
|
|
45
|
+
test2: 2,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
expect(r._params.test1).to.be.eq(1);
|
|
49
|
+
expect(r._params.test2).to.be.eq(2);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('setBaseParam', function() {
|
|
53
|
+
const r = this.repository;
|
|
54
|
+
r.setBaseParam('test', 1);
|
|
55
|
+
|
|
56
|
+
expect(r._baseParams.test).to.be.eq(1);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('setBaseParams', function() {
|
|
60
|
+
const r = this.repository;
|
|
61
|
+
r.setBaseParams({
|
|
62
|
+
test1: 1,
|
|
63
|
+
test2: 2,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
expect(r._baseParams.test1).to.be.eq(1);
|
|
67
|
+
expect(r._baseParams.test2).to.be.eq(2);
|
|
68
|
+
});
|
|
69
|
+
|
|
33
70
|
it('setValuelessParam', function() {
|
|
34
71
|
const r = this.repository;
|
|
35
72
|
r.setParam('conditions[field]', 1);
|
|
@@ -38,6 +75,24 @@ describe('OneBuildRepository', function() {
|
|
|
38
75
|
expect(r._params.conditions.field).to.be.eq(1);
|
|
39
76
|
expect(r._params.conditions[0]).to.be.eq('field IS NOT NULL');
|
|
40
77
|
});
|
|
78
|
+
|
|
79
|
+
it('clearParams', function() {
|
|
80
|
+
const r = this.repository;
|
|
81
|
+
r.setParams({
|
|
82
|
+
test1: 1,
|
|
83
|
+
test2: 2,
|
|
84
|
+
});
|
|
85
|
+
r.setBaseParams({
|
|
86
|
+
test1: 1,
|
|
87
|
+
test2: 2,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
r.clearParams(false, true);
|
|
91
|
+
|
|
92
|
+
expect(r._params).to.be.empty;
|
|
93
|
+
expect(r._baseParams).to.be.empty;
|
|
94
|
+
});
|
|
95
|
+
|
|
41
96
|
});
|
|
42
97
|
|
|
43
98
|
describe('custom', function() {
|
package/package.json
CHANGED
package/src/Repository/Ajax.js
CHANGED
|
@@ -87,9 +87,9 @@ class AjaxRepository extends Repository {
|
|
|
87
87
|
headers: {},
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
|
-
* @member {object}
|
|
90
|
+
* @member {object} _baseParams - Params that will be applied to every request
|
|
91
91
|
*/
|
|
92
|
-
|
|
92
|
+
_baseParams: {},
|
|
93
93
|
|
|
94
94
|
};
|
|
95
95
|
_.merge(this, defaults, config);
|
|
@@ -202,8 +202,7 @@ class AjaxRepository extends Repository {
|
|
|
202
202
|
setParam = (name, value, isBaseParam = false) => {
|
|
203
203
|
const re = /^([^\[]+)\[([^\]]+)\](.*)$/,
|
|
204
204
|
matches = name.match(re),
|
|
205
|
-
paramsToChange = isBaseParam ? this.
|
|
206
|
-
|
|
205
|
+
paramsToChange = isBaseParam ? this._baseParams : this._params;
|
|
207
206
|
|
|
208
207
|
if (matches) { // name has array notation like 'conditions[username]'
|
|
209
208
|
const first = matches[1],
|
|
@@ -234,7 +233,7 @@ class AjaxRepository extends Repository {
|
|
|
234
233
|
setValuelessParam = (name, isBaseParam = false) => {
|
|
235
234
|
const re = /^([^\[]+)\[([^\]]+)\](.*)$/,
|
|
236
235
|
matches = name.match(re),
|
|
237
|
-
paramsToChange = isBaseParam ? this.
|
|
236
|
+
paramsToChange = isBaseParam ? this._baseParams : this._params;
|
|
238
237
|
|
|
239
238
|
if (matches) { // name has array notation like 'conditions[username]'
|
|
240
239
|
const first = matches[1],
|
|
@@ -262,24 +261,35 @@ class AjaxRepository extends Repository {
|
|
|
262
261
|
});
|
|
263
262
|
}
|
|
264
263
|
|
|
264
|
+
/**
|
|
265
|
+
* Sets base query param
|
|
266
|
+
* @param {string} name - Param name to set.
|
|
267
|
+
* @param {any} value - Param value to set.
|
|
268
|
+
*/
|
|
269
|
+
setBaseParam = (name, value) => {
|
|
270
|
+
this.setParam(name, value, true);
|
|
271
|
+
}
|
|
272
|
+
|
|
265
273
|
/**
|
|
266
274
|
* Sets base query params. These params are sent on every request.
|
|
267
275
|
* @param {object} params - Base params to set. Key is parameter name, value is parameter value
|
|
268
276
|
*/
|
|
269
277
|
setBaseParams = (params) => {
|
|
270
278
|
_.each(params, (value, name) => {
|
|
271
|
-
this.
|
|
279
|
+
this.setBaseParam(name, value);
|
|
272
280
|
});
|
|
273
281
|
}
|
|
274
282
|
|
|
275
283
|
/**
|
|
276
|
-
* Manually clears all
|
|
277
|
-
* including sorting, filtering, and pagination.
|
|
284
|
+
* Manually clears all (non-base) params including filtering.
|
|
278
285
|
* *Not intended for normal usage,* but rather for testing.
|
|
279
286
|
* @param {boolean} reload - Whether to reload repository. Defaults to false.
|
|
280
287
|
*/
|
|
281
|
-
clearParams = (reload = false) => {
|
|
288
|
+
clearParams = (reload = false, clearBase = false) => {
|
|
282
289
|
this._params = {};
|
|
290
|
+
if (clearBase) {
|
|
291
|
+
this._baseParams = {};
|
|
292
|
+
}
|
|
283
293
|
if (reload && this.isLoaded) {
|
|
284
294
|
return this.reload();
|
|
285
295
|
}
|
|
@@ -292,8 +302,8 @@ class AjaxRepository extends Repository {
|
|
|
292
302
|
*/
|
|
293
303
|
_onChangeSorters = () => {
|
|
294
304
|
const sorter = this.sorters[0];
|
|
295
|
-
this.
|
|
296
|
-
this.
|
|
305
|
+
this.setBaseParam(this.paramSort, sorter.name);
|
|
306
|
+
this.setBaseParam(this.paramDirection, sorter.direction);
|
|
297
307
|
|
|
298
308
|
if (this.isLoaded) {
|
|
299
309
|
return this.reload();
|
|
@@ -319,8 +329,8 @@ class AjaxRepository extends Repository {
|
|
|
319
329
|
* Refreshes entities.
|
|
320
330
|
*/
|
|
321
331
|
_onChangePagination = () => {
|
|
322
|
-
this.
|
|
323
|
-
this.
|
|
332
|
+
this.setBaseParam(this.paramPageNum, this.page);
|
|
333
|
+
this.setBaseParam(this.paramPageSize, this.pageSize);
|
|
324
334
|
|
|
325
335
|
if (this.isLoaded) {
|
|
326
336
|
return this.reload();
|
|
@@ -360,7 +370,7 @@ class AjaxRepository extends Repository {
|
|
|
360
370
|
}
|
|
361
371
|
|
|
362
372
|
const repository = this;
|
|
363
|
-
const data = _.assign({}, this.
|
|
373
|
+
const data = _.assign({}, this._baseParams, this._params);
|
|
364
374
|
|
|
365
375
|
return this._send(this.methods.get, this.api.get, data)
|
|
366
376
|
.then(result => {
|
|
@@ -462,7 +472,7 @@ class AjaxRepository extends Repository {
|
|
|
462
472
|
const params = {
|
|
463
473
|
id: entity.id,
|
|
464
474
|
};
|
|
465
|
-
return _.assign({}, this.
|
|
475
|
+
return _.assign({}, this._baseParams, params);
|
|
466
476
|
}
|
|
467
477
|
|
|
468
478
|
/**
|