-
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 #36 from fleetbase/dev-v0.2.7
v0.2.7
- Loading branch information
Showing
7 changed files
with
72 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,33 @@ | ||
import getModelName from '@fleetbase/ember-core/utils/get-model-name'; | ||
import isModel from '@fleetbase/ember-core/utils/is-model'; | ||
import { camelize } from '@ember/string'; | ||
|
||
/** | ||
* Applies context and dynamic arguments to a given component. | ||
* | ||
* @param {Component} component - The component to which context and arguments will be applied. | ||
*/ | ||
export default function applyContextComponentArguments(component) { | ||
const { context, dynamicArgs = {} } = component.args; | ||
|
||
// Apply context model if available | ||
if (context && isModel(context)) { | ||
const contextModelName = camelize(getModelName(context)); | ||
if (contextModelName) { | ||
component[contextModelName] = context; | ||
} | ||
} | ||
|
||
// Execute any apply callback present in dynamic arguments | ||
const { applyCallback } = dynamicArgs; | ||
if (typeof applyCallback === 'function') { | ||
applyCallback(component); | ||
} | ||
|
||
// Apply other dynamic arguments to the component | ||
for (const [key, value] of Object.entries(dynamicArgs)) { | ||
if (key !== 'applyCallback') { | ||
component[key] = value; | ||
} | ||
} | ||
} |
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,16 @@ | ||
export default function contextComponentCallback(component, name, ...params) { | ||
let callbackInvoked = false; | ||
|
||
if (typeof component.args[name] === 'function') { | ||
component.args[name](...params); | ||
callbackInvoked = true; | ||
} | ||
|
||
// now do for context options | ||
if (typeof component.args.options === 'object' && typeof component.args.options[name] === 'function') { | ||
component.args.options[name](...params); | ||
callbackInvoked = true; | ||
} | ||
|
||
return callbackInvoked; | ||
} |
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/utils/apply-context-component-arguments'; |
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/utils/context-component-callback'; |
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
10 changes: 10 additions & 0 deletions
10
tests/unit/utils/apply-context-component-arguments-test.js
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,10 @@ | ||
import applyContextComponentArguments from 'dummy/utils/apply-context-component-arguments'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Unit | Utility | apply-context-component-arguments', function () { | ||
// TODO: Replace this with your real tests. | ||
test('it works', function (assert) { | ||
let result = applyContextComponentArguments(); | ||
assert.ok(result); | ||
}); | ||
}); |
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,10 @@ | ||
import contextComponentCallback from 'dummy/utils/context-component-callback'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Unit | Utility | context-component-callback', function () { | ||
// TODO: Replace this with your real tests. | ||
test('it works', function (assert) { | ||
let result = contextComponentCallback(); | ||
assert.ok(result); | ||
}); | ||
}); |