Skip to content

Commit

Permalink
Merge pull request #36 from fleetbase/dev-v0.2.7
Browse files Browse the repository at this point in the history
v0.2.7
  • Loading branch information
roncodes authored Mar 14, 2024
2 parents ec10c85 + fc76cf8 commit a9766de
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 1 deletion.
33 changes: 33 additions & 0 deletions addon/utils/apply-context-component-arguments.js
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;
}
}
}
16 changes: 16 additions & 0 deletions addon/utils/context-component-callback.js
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;
}
1 change: 1 addition & 0 deletions app/utils/apply-context-component-arguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/ember-core/utils/apply-context-component-arguments';
1 change: 1 addition & 0 deletions app/utils/context-component-callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/ember-core/utils/context-component-callback';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/utils/apply-context-component-arguments-test.js
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);
});
});
10 changes: 10 additions & 0 deletions tests/unit/utils/context-component-callback-test.js
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);
});
});

0 comments on commit a9766de

Please sign in to comment.