Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX beta] Change preference in positional parameters #12350

Merged
merged 1 commit into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions packages/ember-htmlbars/lib/node-managers/component-node-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,44 @@ function processPositionalParams(renderNode, positionalParams, params, attrs) {
// if the component is rendered via {{component}} helper, the first
// element of `params` is the name of the component, so we need to
// skip that when the positional parameters are constructed
const paramsStartIndex = renderNode.getState().isComponentHelper ? 1 : 0;
const isNamed = typeof positionalParams === 'string';
let paramsStream;

if (isNamed) {
paramsStream = new Stream(() => {
return readArray(params.slice(paramsStartIndex));
}, 'params');
processRestPositionalParameters(renderNode, positionalParams, params, attrs);
} else {
processNamedPositionalParameters(renderNode, positionalParams, params, attrs);
}
}

attrs[positionalParams] = paramsStream;
function processNamedPositionalParameters(renderNode, positionalParams, params, attrs) {
const paramsStartIndex = renderNode.getState().isComponentHelper ? 1 : 0;

for (let i = 0; i < positionalParams.length; i++) {
let param = params[paramsStartIndex + i];

assert(`You cannot specify both a positional param (at position ${i}) and the hash argument \`${positionalParams[i]}\`.`,
!(positionalParams[i] in attrs));

attrs[positionalParams[i]] = param;
}
}

if (isNamed) {
for (let i = paramsStartIndex; i < params.length; i++) {
let param = params[i];
paramsStream.addDependency(param);
}
} else {
for (let i = 0; i < positionalParams.length; i++) {
let param = params[paramsStartIndex + i];
attrs[positionalParams[i]] = param;
}
function processRestPositionalParameters(renderNode, positionalParamsName, params, attrs) {
// If there is already an attribute for that variable, do nothing
assert(`You cannot specify positional parameters and the hash argument \`${positionalParamsName}\`.`,
!(positionalParamsName in attrs));

const paramsStartIndex = renderNode.getState().isComponentHelper ? 1 : 0;

let paramsStream = new Stream(() => {
return readArray(params.slice(paramsStartIndex));
}, 'params');

attrs[positionalParamsName] = paramsStream;

for (let i = paramsStartIndex; i < params.length; i++) {
let param = params[i];
paramsStream.addDependency(param);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,29 @@ QUnit.test('dynamic named positional parameters', function() {
equal(jQuery('#qunit-fixture').text(), 'Edward5');
});

QUnit.test('if a value is passed as a non-positional parameter, it takes precedence over the named one', function() {
var SampleComponent = Component.extend();
SampleComponent.reopenClass({
positionalParams: ['name']
});

registry.register('template:components/sample-component', compile('{{attrs.name}}'));
registry.register('component:sample-component', SampleComponent);

view = EmberView.extend({
layout: compile('{{sample-component notMyName name=myName}}'),
container: container,
context: {
myName: 'Quint',
notMyName: 'Sergio'
}
}).create();

expectAssertion(function() {
runAppend(view);
}, `You cannot specify both a positional param (at position 0) and the hash argument \`name\`.`);
});

QUnit.test('static arbitrary number of positional parameters', function() {
var SampleComponent = Component.extend();
SampleComponent.reopenClass({
Expand All @@ -406,6 +429,28 @@ QUnit.test('static arbitrary number of positional parameters', function() {
equal(view.$('#helper').text(), 'Foo4Bar5Baz');
});

QUnit.test('arbitrary positional parameter conflict with hash parameter is reported', function() {
var SampleComponent = Component.extend();
SampleComponent.reopenClass({
positionalParams: 'names'
});

registry.register('template:components/sample-component', compile('{{#each attrs.names as |name|}}{{name}}{{/each}}'));
registry.register('component:sample-component', SampleComponent);

view = EmberView.extend({
layout: compile('{{sample-component "Foo" 4 "Bar" names=numbers id="args-3"}}'),
container: container,
context: {
numbers: [1, 2, 3]
}
}).create();

expectAssertion(function() {
runAppend(view);
}, `You cannot specify positional parameters and the hash argument \`names\`.`);
});

QUnit.test('dynamic arbitrary number of positional parameters', function() {
var SampleComponent = Component.extend();
SampleComponent.reopenClass({
Expand Down