From fc61b95a9e2c89a979904f8a27a195833208d6a5 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Sun, 18 Aug 2024 18:12:48 +0800 Subject: [PATCH 1/2] added back legacy decorators for fetch and store fetching which worked great on controllers --- addon/decorators/legacy-fetch-from.js | 55 +++++++++++++++++++++++++++ addon/decorators/legacy-from-store.js | 52 +++++++++++++++++++++++++ app/decorators/legacy-fetch-from.js | 1 + app/decorators/legacy-from-store.js | 1 + package.json | 2 +- 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 addon/decorators/legacy-fetch-from.js create mode 100644 addon/decorators/legacy-from-store.js create mode 100644 app/decorators/legacy-fetch-from.js create mode 100644 app/decorators/legacy-from-store.js diff --git a/addon/decorators/legacy-fetch-from.js b/addon/decorators/legacy-fetch-from.js new file mode 100644 index 0000000..38a0ea9 --- /dev/null +++ b/addon/decorators/legacy-fetch-from.js @@ -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); +} \ No newline at end of file diff --git a/addon/decorators/legacy-from-store.js b/addon/decorators/legacy-from-store.js new file mode 100644 index 0000000..8943cfe --- /dev/null +++ b/addon/decorators/legacy-from-store.js @@ -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; +}); \ No newline at end of file diff --git a/app/decorators/legacy-fetch-from.js b/app/decorators/legacy-fetch-from.js new file mode 100644 index 0000000..8a39ba1 --- /dev/null +++ b/app/decorators/legacy-fetch-from.js @@ -0,0 +1 @@ +export { default } from '@fleetbase/ember-core/decorators/legacy-fetch-from'; diff --git a/app/decorators/legacy-from-store.js b/app/decorators/legacy-from-store.js new file mode 100644 index 0000000..e000d1b --- /dev/null +++ b/app/decorators/legacy-from-store.js @@ -0,0 +1 @@ +export { default } from '@fleetbase/ember-core/decorators/legacy-from-store'; \ No newline at end of file diff --git a/package.json b/package.json index da6b9c3..214097a 100644 --- a/package.json +++ b/package.json @@ -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", From 4559a41d5f13127980711f26a20ff34517867491 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Sun, 18 Aug 2024 18:15:41 +0800 Subject: [PATCH 2/2] fixed linter --- addon/decorators/legacy-fetch-from.js | 2 +- addon/decorators/legacy-from-store.js | 2 +- app/decorators/legacy-from-store.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/addon/decorators/legacy-fetch-from.js b/addon/decorators/legacy-fetch-from.js index 38a0ea9..1b2eb6c 100644 --- a/addon/decorators/legacy-fetch-from.js +++ b/addon/decorators/legacy-fetch-from.js @@ -52,4 +52,4 @@ export default function fetchFrom(endpoint, query = {}, options = {}) { }); }; }, 'fetchFrom')(endpoint, query, options); -} \ No newline at end of file +} diff --git a/addon/decorators/legacy-from-store.js b/addon/decorators/legacy-from-store.js index 8943cfe..29d60b3 100644 --- a/addon/decorators/legacy-from-store.js +++ b/addon/decorators/legacy-from-store.js @@ -49,4 +49,4 @@ export default decoratorWithRequiredParams(function (target, key, descriptor, [m }; return descriptor; -}); \ No newline at end of file +}); diff --git a/app/decorators/legacy-from-store.js b/app/decorators/legacy-from-store.js index e000d1b..349d114 100644 --- a/app/decorators/legacy-from-store.js +++ b/app/decorators/legacy-from-store.js @@ -1 +1 @@ -export { default } from '@fleetbase/ember-core/decorators/legacy-from-store'; \ No newline at end of file +export { default } from '@fleetbase/ember-core/decorators/legacy-from-store';