@fleetbase/ember-core 0.2.15 → 0.2.16
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.
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { decoratorWithRequiredParams } from '@ember-decorators/utils/decorator';
|
|
2
|
+
import { assert } from '@ember/debug';
|
|
3
|
+
import { getOwner } from '@ember/application';
|
|
4
|
+
import { scheduleOnce } from '@ember/runloop';
|
|
5
|
+
|
|
6
|
+
export default function fetchFrom(endpoint, query = {}, options = {}) {
|
|
7
|
+
assert('The first argument of the @fetchFrom decorator must be a string', typeof endpoint === 'string');
|
|
8
|
+
assert('The second argument of the @fetchFrom decorator must be an object', typeof query === 'object');
|
|
9
|
+
assert('The third argument of the @fetchFrom decorator must be an object', typeof options === 'object');
|
|
10
|
+
|
|
11
|
+
return decoratorWithRequiredParams(function (target, key) {
|
|
12
|
+
const symbol = Symbol(`__${key}_fetchFrom`);
|
|
13
|
+
|
|
14
|
+
Object.defineProperty(target, symbol, {
|
|
15
|
+
configurable: true,
|
|
16
|
+
enumerable: false,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: null,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
Object.defineProperty(target, key, {
|
|
22
|
+
configurable: true,
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get() {
|
|
25
|
+
return this[symbol];
|
|
26
|
+
},
|
|
27
|
+
set(value) {
|
|
28
|
+
this[symbol] = value;
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const originalInit = target.init;
|
|
33
|
+
|
|
34
|
+
target.init = function () {
|
|
35
|
+
if (originalInit) {
|
|
36
|
+
originalInit.call(this);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
scheduleOnce('afterRender', this, function () {
|
|
40
|
+
const owner = getOwner(this);
|
|
41
|
+
const fetch = owner.lookup('service:fetch'); // Get the Fleetbase Fetch service
|
|
42
|
+
|
|
43
|
+
// Perform the query and set the result to the property
|
|
44
|
+
fetch
|
|
45
|
+
.get(endpoint, query, options)
|
|
46
|
+
.then((result) => {
|
|
47
|
+
this.set(key, result);
|
|
48
|
+
})
|
|
49
|
+
.catch(() => {
|
|
50
|
+
this.set(key, []);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
}, 'fetchFrom')(endpoint, query, options);
|
|
55
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { decoratorWithRequiredParams } from '@ember-decorators/utils/decorator';
|
|
2
|
+
import { getOwner } from '@ember/application';
|
|
3
|
+
import { assert } from '@ember/debug';
|
|
4
|
+
|
|
5
|
+
export default decoratorWithRequiredParams(function (target, key, descriptor, [modelName, query = {}, options = {}]) {
|
|
6
|
+
assert('The first argument of the @fetchFrom decorator must be a string', typeof modelName === 'string');
|
|
7
|
+
assert('The second argument of the @fetchFrom decorator must be an object', typeof query === 'object');
|
|
8
|
+
assert('The third argument of the @fetchFrom decorator must be an object', typeof options === 'object');
|
|
9
|
+
|
|
10
|
+
// Remove value and writable if previously set, use getter instead
|
|
11
|
+
delete descriptor.value;
|
|
12
|
+
delete descriptor.writable;
|
|
13
|
+
delete descriptor.initializer;
|
|
14
|
+
|
|
15
|
+
// Create symbol to track value
|
|
16
|
+
const symbol = Symbol(`__${key}_fromStore`);
|
|
17
|
+
|
|
18
|
+
// Setter to get symbol value
|
|
19
|
+
descriptor.set = function (value) {
|
|
20
|
+
this[symbol] = value;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// Get or set symbol value
|
|
24
|
+
descriptor.get = function () {
|
|
25
|
+
if (this[symbol] !== undefined) {
|
|
26
|
+
return this[symbol];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
Object.defineProperty(this, symbol, {
|
|
30
|
+
configurable: true,
|
|
31
|
+
enumerable: false,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: null,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const owner = getOwner(this);
|
|
37
|
+
const store = owner.lookup('service:store');
|
|
38
|
+
return store
|
|
39
|
+
.query(modelName, query, options)
|
|
40
|
+
.then((response) => {
|
|
41
|
+
this.set(key, response);
|
|
42
|
+
if (options && typeof options.onComplete === 'function') {
|
|
43
|
+
options.onComplete(response, this);
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
.catch(() => {
|
|
47
|
+
this.set(key, null);
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return descriptor;
|
|
52
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from '@fleetbase/ember-core/decorators/legacy-fetch-from';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from '@fleetbase/ember-core/decorators/legacy-from-store';
|
package/package.json
CHANGED