-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from fleetbase/dev-v0.2.16
v0.2.16
- Loading branch information
Showing
5 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@fleetbase/ember-core/decorators/legacy-fetch-from'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@fleetbase/ember-core/decorators/legacy-from-store'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters