Skip to content

Commit

Permalink
Merge pull request #11264 from rwjblue/add-concat-helper
Browse files Browse the repository at this point in the history
Expose `{{concat}}` helper publicly.
  • Loading branch information
rwjblue committed Jun 6, 2015
2 parents 73acd45 + 9e8fd04 commit 55f02b1
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 9 deletions.
21 changes: 16 additions & 5 deletions packages/ember-htmlbars/lib/helpers/-concat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
/** @private
This private helper is used by the legacy class bindings AST transformer
to concatenate class names together.
/**
Concatenates input params together.
Example:
```handlebars
{{some-component name=(concat firstName " " lastName)}}
{{! would pass name="<first name value> <last name value>" to the component}}
```
@public
@method concat
@for Ember.HTMLBars
*/
export default function concat(params, hash) {
return params.join(hash.separator);
export default function concat(params) {
return params.join('');
}
2 changes: 1 addition & 1 deletion packages/ember-htmlbars/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ if (Ember.FEATURES.isEnabled('ember-htmlbars-each-in')) {
}
registerHelper('-bind-attr-class', bindAttrClassHelper);
registerHelper('-normalize-class', normalizeClassHelper);
registerHelper('-concat', concatHelper);
registerHelper('concat', concatHelper);
registerHelper('-join-classes', joinClassesHelper);
registerHelper('-legacy-each-with-controller', legacyEachWithControllerHelper);
registerHelper('-legacy-each-with-keyword', legacyEachWithKeywordHelper);
Expand Down
87 changes: 87 additions & 0 deletions packages/ember-htmlbars/tests/helpers/concat-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import run from "ember-metal/run_loop";
import { Registry } from "ember-runtime/system/container";
import Component from "ember-views/views/component";
import compile from "ember-template-compiler/system/compile";

import { runAppend, runDestroy } from "ember-runtime/tests/utils";

var component, registry, container;

QUnit.module("ember-htmlbars: {{concat}} helper", {
setup() {
registry = new Registry();
container = registry.container();
registry.optionsForType('helper', { instantiate: false });
},

teardown() {
runDestroy(container);
runDestroy(component);
}
});

QUnit.test('concats provided params', function() {
component = Component.create({
container,

template: compile(`{{concat "foo" " " "bar" " " "baz"}}`)
});

runAppend(component);

equal(component.$().text(), 'foo bar baz');
});

QUnit.test('updates for bound params', function() {
component = Component.create({
container,

firstParam: 'one',
secondParam: 'two',

template: compile(`{{concat firstParam secondParam}}`)
});

runAppend(component);

equal(component.$().text(), 'onetwo');

run(function() {
component.set('firstParam', 'three');
});

equal(component.$().text(), 'threetwo');

run(function() {
component.set('secondParam', 'four');
});

equal(component.$().text(), 'threefour');
});

QUnit.test('can be used as a sub-expression', function() {
function eq([ actual, expected ]) {
return actual === expected;
}
eq.isHTMLBars = true;
registry.register('helper:x-eq', eq);

component = Component.create({
container,

firstParam: 'one',
secondParam: 'two',

template: compile(`{{#if (x-eq (concat firstParam secondParam) "onetwo")}}Truthy!{{else}}False{{/if}}`)
});

runAppend(component);

equal(component.$().text(), 'Truthy!');

run(function() {
component.set('firstParam', 'three');
});

equal(component.$().text(), 'False');
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ TransformOldClassBindingSyntax.prototype.transform = function TransformOldClassB

if (classPair) {
classValue.push(classPair.value);
classValue.push(b.string(' '));
} else {
classPair = b.pair('class', null);
node.hash.pairs.push(classPair);
Expand All @@ -54,8 +55,8 @@ TransformOldClassBindingSyntax.prototype.transform = function TransformOldClassB
}
});

let hash = b.hash([b.pair('separator', b.string(' '))]);
classPair.value = b.sexpr(b.string('-concat'), classValue, hash);
let hash = b.hash();
classPair.value = b.sexpr(b.string('concat'), classValue, hash);
});

return ast;
Expand Down Expand Up @@ -97,6 +98,7 @@ function buildSexprs(microsyntax, sexprs, b) {
}

sexprs.push(sexpr);
sexprs.push(b.string(' '));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function normalizeComponentAttributes(component, isAngleBracket, attrs) {
var existingStyle = normalized.style;

if (existingStyle) {
normalized.style = ['subexpr', '-concat', [existingStyle, hiddenStyle], ['separator', ' ']];
normalized.style = ['subexpr', 'concat', [existingStyle, ' ', hiddenStyle], [ ]];
} else {
normalized.style = hiddenStyle;
}
Expand Down

0 comments on commit 55f02b1

Please sign in to comment.