Skip to content

Commit

Permalink
Add basic nesting for closure components
Browse files Browse the repository at this point in the history
  • Loading branch information
Serabe committed Sep 16, 2015
1 parent c4fc229 commit 7fabb60
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
1 change: 0 additions & 1 deletion packages/ember-htmlbars/lib/hooks/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export default function componentHook(renderNode, env, scope, _tagName, params,
// Invoking a component from the outside (either via <foo-bar> angle brackets
// or {{foo-bar}} legacy curlies).

console.log(`HASH TO CNM`, attrs);
var manager = ComponentNodeManager.create(renderNode, env, {
tagName,
params,
Expand Down
21 changes: 20 additions & 1 deletion packages/ember-htmlbars/lib/keywords/closure-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { symbol } from 'ember-metal/utils';
import BasicStream from 'ember-metal/streams/stream';
import { read } from 'ember-metal/streams/utils';
import { labelForSubexpr } from 'ember-htmlbars/hooks/subexpr';
import assign from 'ember-metal/assign';

export const COMPONENT_REFERENCE = symbol('COMPONENT_REFERENCE');
export const COMPONENT_CELL = symbol('COMPONENT_CELL');
Expand Down Expand Up @@ -51,7 +52,9 @@ function createClosureComponentCell(env, originalComponentPath, params, hash) {
if (componentPath && componentPath[COMPONENT_CELL]) {
val = {
[COMPONENT_PATH]: componentPath[COMPONENT_PATH],
[COMPONENT_PARAMS]: []
[COMPONENT_PARAMS]: mergeParams(componentPath[COMPONENT_PARAMS], params),
[COMPONENT_HASH]: mergeHash(componentPath[COMPONENT_HASH], hash),
[COMPONENT_CELL]: true
};
} else {
val = {
Expand All @@ -64,3 +67,19 @@ function createClosureComponentCell(env, originalComponentPath, params, hash) {

return val;
}

export function mergeParams(original, update) {
// If update has the same or more items than original, we can just return the
// update
if (update.length >= original.length) {
return update;
} else {
let result = update.slice(0);
result.push(...original.slice(update.length));
return result;
}
}

export function mergeHash(original, updates) {
return assign(original, updates);
}
18 changes: 2 additions & 16 deletions packages/ember-htmlbars/lib/keywords/element-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
COMPONENT_PATH,
COMPONENT_PARAMS,
COMPONENT_HASH,
mergeHash,
mergeParams
} from './closure-component';

export default {
Expand Down Expand Up @@ -48,12 +50,8 @@ function render(morph, env, scope, [path, ...params], hash, template, inverse, v

if (path && path[COMPONENT_REFERENCE]) {
let closureComponent = env.hooks.getValue(path);
console.log(`PARAMS`, params, closureComponent[COMPONENT_PARAMS]);
params = mergeParams(closureComponent[COMPONENT_PARAMS], params);
console.log(params);
console.log(`HASH`, hash, closureComponent[COMPONENT_HASH]);
hash = mergeHash(closureComponent[COMPONENT_HASH], hash);
console.log(hash);
}

// If the value passed to the {{component}} helper is undefined or null,
Expand All @@ -68,15 +66,3 @@ function render(morph, env, scope, [path, ...params], hash, template, inverse, v
params, hash, templates, visitor
);
}

function mergeParams(...paramsArray) {
let mergedParams = [];
for (let i = 0; i < paramsArray.length; i++) {
mergedParams.splice(mergedParams.length, 0, ...paramsArray[i]);
}
return mergedParams;
}

function mergeHash(...hashArray) {
return assign({}, ...hashArray);
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ ComponentNodeManager.create = function ComponentNodeManager_create(renderNode, e
createOptions._controller = getValue(parentScope.getLocal('controller'));
}

console.log(`before extractPositionalParams`, component, params, attrs);
extractPositionalParams(renderNode, component, params, attrs);
console.log(`after extractPositionalParams`, component, params, attrs);

// Instantiate the component
component = createComponent(component, isAngleBracket, createOptions, renderNode, env, attrs);
Expand Down
48 changes: 48 additions & 0 deletions packages/ember-htmlbars/tests/helpers/closure_component_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,52 @@ if (Ember.FEATURES.isEnabled('ember-contextual-components')) {
equal(component.$().text(), `Hodi`,
'greeting is bound');
});

QUnit.test('nested components overwrites named positional parameters', function() {
let LookedUp = Component.extend();
LookedUp.reopenClass({
positionalParams: ['name', 'age']
});
registry.register(
'component:-looked-up',
LookedUp
);
registry.register(
'template:components/-looked-up',
compile(`{{name}} {{age}}`)
);

let template = compile(
`{{component
(component (component "-looked-up" "Sergio" 28)
"Marvin" 21)
"Hodari"}}`
);
component = Component.extend({ container, template }).create();

runAppend(component);
equal(component.$().text(), 'Hodari 21', '-looked-up component rendered');
});

QUnit.test('nested components overwrites hash parameters', function() {
registry.register(
'template:components/-looked-up',
compile(`{{greeting}} {{name}} {{age}}`)
);

let template = compile(
`{{component (component (component "-looked-up"
greeting="Hola" name="Dolores" age=33)
greeting="Hej" name="Sigmundur")
greeting=greeting}}`
);
component = Component.extend({ container, template, greeting: 'Hodi' }).create();

runAppend(component);

equal(component.$().text(), 'Hodi Sigmundur 33', '-looked-up component rendered');
});

// Avoid assertions
// Add tests for bound properties
}

0 comments on commit 7fabb60

Please sign in to comment.