Skip to content

Commit

Permalink
added back legacy decorators for fetch and store fetching which worke…
Browse files Browse the repository at this point in the history
…d great on controllers
  • Loading branch information
roncodes committed Aug 18, 2024
1 parent e875bf5 commit fc61b95
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
55 changes: 55 additions & 0 deletions addon/decorators/legacy-fetch-from.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { decoratorWithRequiredParams } from '@ember-decorators/utils/decorator';
import { assert } from '@ember/debug';
import { getOwner } from '@ember/application';
import { scheduleOnce } from '@ember/runloop';

export default function fetchFrom(endpoint, query = {}, options = {}) {
assert('The first argument of the @fetchFrom decorator must be a string', typeof endpoint === 'string');
assert('The second argument of the @fetchFrom decorator must be an object', typeof query === 'object');
assert('The third argument of the @fetchFrom decorator must be an object', typeof options === 'object');

return decoratorWithRequiredParams(function (target, key) {
const symbol = Symbol(`__${key}_fetchFrom`);

Object.defineProperty(target, symbol, {
configurable: true,
enumerable: false,
writable: true,
value: null,
});

Object.defineProperty(target, key, {
configurable: true,
enumerable: true,
get() {
return this[symbol];
},
set(value) {
this[symbol] = value;
},
});

const originalInit = target.init;

target.init = function () {
if (originalInit) {
originalInit.call(this);
}

scheduleOnce('afterRender', this, function () {
const owner = getOwner(this);
const fetch = owner.lookup('service:fetch'); // Get the Fleetbase Fetch service

// Perform the query and set the result to the property
fetch
.get(endpoint, query, options)
.then((result) => {
this.set(key, result);
})
.catch(() => {
this.set(key, []);
});
});
};
}, 'fetchFrom')(endpoint, query, options);
}
52 changes: 52 additions & 0 deletions addon/decorators/legacy-from-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { decoratorWithRequiredParams } from '@ember-decorators/utils/decorator';
import { getOwner } from '@ember/application';
import { assert } from '@ember/debug';

export default decoratorWithRequiredParams(function (target, key, descriptor, [modelName, query = {}, options = {}]) {
assert('The first argument of the @fetchFrom decorator must be a string', typeof modelName === 'string');
assert('The second argument of the @fetchFrom decorator must be an object', typeof query === 'object');
assert('The third argument of the @fetchFrom decorator must be an object', typeof options === 'object');

// Remove value and writable if previously set, use getter instead
delete descriptor.value;
delete descriptor.writable;
delete descriptor.initializer;

// Create symbol to track value
const symbol = Symbol(`__${key}_fromStore`);

// Setter to get symbol value
descriptor.set = function (value) {
this[symbol] = value;
};

// Get or set symbol value
descriptor.get = function () {
if (this[symbol] !== undefined) {
return this[symbol];
}

Object.defineProperty(this, symbol, {
configurable: true,
enumerable: false,
writable: true,
value: null,
});

const owner = getOwner(this);
const store = owner.lookup('service:store');
return store
.query(modelName, query, options)
.then((response) => {
this.set(key, response);
if (options && typeof options.onComplete === 'function') {
options.onComplete(response, this);
}
})
.catch(() => {
this.set(key, null);
});
};

return descriptor;
});
1 change: 1 addition & 0 deletions app/decorators/legacy-fetch-from.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/ember-core/decorators/legacy-fetch-from';
1 change: 1 addition & 0 deletions app/decorators/legacy-from-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/ember-core/decorators/legacy-from-store';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fleetbase/ember-core",
"version": "0.2.15",
"version": "0.2.16",
"description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
"keywords": [
"fleetbase-core",
Expand Down

0 comments on commit fc61b95

Please sign in to comment.