diff --git a/addon/utils/apply-context-component-arguments.js b/addon/utils/apply-context-component-arguments.js new file mode 100644 index 0000000..3b74145 --- /dev/null +++ b/addon/utils/apply-context-component-arguments.js @@ -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; + } + } +} diff --git a/addon/utils/context-component-callback.js b/addon/utils/context-component-callback.js new file mode 100644 index 0000000..a31c2a3 --- /dev/null +++ b/addon/utils/context-component-callback.js @@ -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; +} diff --git a/app/utils/apply-context-component-arguments.js b/app/utils/apply-context-component-arguments.js new file mode 100644 index 0000000..d5a4d37 --- /dev/null +++ b/app/utils/apply-context-component-arguments.js @@ -0,0 +1 @@ +export { default } from '@fleetbase/ember-core/utils/apply-context-component-arguments'; diff --git a/app/utils/context-component-callback.js b/app/utils/context-component-callback.js new file mode 100644 index 0000000..a114b70 --- /dev/null +++ b/app/utils/context-component-callback.js @@ -0,0 +1 @@ +export { default } from '@fleetbase/ember-core/utils/context-component-callback'; diff --git a/package.json b/package.json index bdc9c41..d1c5978 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fleetbase/ember-core", - "version": "0.2.6", + "version": "0.2.7", "description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.", "keywords": [ "fleetbase-core", diff --git a/tests/unit/utils/apply-context-component-arguments-test.js b/tests/unit/utils/apply-context-component-arguments-test.js new file mode 100644 index 0000000..17998ef --- /dev/null +++ b/tests/unit/utils/apply-context-component-arguments-test.js @@ -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); + }); +}); diff --git a/tests/unit/utils/context-component-callback-test.js b/tests/unit/utils/context-component-callback-test.js new file mode 100644 index 0000000..b0855fb --- /dev/null +++ b/tests/unit/utils/context-component-callback-test.js @@ -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); + }); +});