diff --git a/addon/decorators/legacy-fetch-from.js b/addon/decorators/legacy-fetch-from.js new file mode 100644 index 0000000..1b2eb6c --- /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); +} diff --git a/addon/decorators/legacy-from-store.js b/addon/decorators/legacy-from-store.js new file mode 100644 index 0000000..29d60b3 --- /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; +}); 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..349d114 --- /dev/null +++ b/app/decorators/legacy-from-store.js @@ -0,0 +1 @@ +export { default } from '@fleetbase/ember-core/decorators/legacy-from-store'; 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",