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 Release] Special case {{#with}} for isTruthy #12922

Merged
merged 1 commit into from
Feb 7, 2016
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
15 changes: 12 additions & 3 deletions packages/ember-htmlbars/lib/hooks/link-render-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export default function linkRenderNode(renderNode, env, scope, path, params, has
switch (path) {
case 'unbound': return true;
case 'unless':
case 'if': params[0] = shouldDisplay(params[0]); break;
case 'if': params[0] = shouldDisplay(params[0], toBool); break;
case 'each': params[0] = eachParam(params[0]); break;
case 'with': params[0] = shouldDisplay(params[0], identity); break;
}
}

Expand Down Expand Up @@ -76,7 +77,7 @@ function eachParam(list) {
return stream;
}

function shouldDisplay(predicate) {
function shouldDisplay(predicate, coercer) {
let length = getKey(predicate, 'length');
let isTruthy = getKey(predicate, 'isTruthy');

Expand All @@ -93,7 +94,7 @@ function shouldDisplay(predicate) {
return isTruthyVal;
}

return !!predicateVal;
return coercer(predicateVal);
}, 'ShouldDisplay');

addDependency(stream, length);
Expand All @@ -102,6 +103,14 @@ function shouldDisplay(predicate) {
return stream;
}

function toBool(value) {
return !!value;
}

function identity(value) {
return value;
}

function getKey(obj, key) {
if (isStream(obj)) {
return obj.getKey(key);
Expand Down
73 changes: 73 additions & 0 deletions packages/ember-htmlbars/tests/helpers/with_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,79 @@ QUnit.test('re-using the same variable with different #with blocks does not over
equal(view.$().text(), 'Admin: Tom Dale User: Yehuda Katz', 'should be properly scoped');
});

QUnit.test('should respect `isTruthy` field on a view', function() {
view = EmberView.create({
template: compile('{{#with view}}True{{else}}False{{/with}}'),
isTruthy: true
});
runAppend(view);

equal(view.$().text(), 'True');

run(function() {
set(view, 'isTruthy', false);
});

equal(view.$().text(), 'False');

run(function() {
set(view, 'isTruthy', true);
});

equal(view.$().text(), 'True');
});

QUnit.test('should respect `isTruthy` field on an object', function() {
view = EmberView.create({
template: compile('{{#with view.foo}}True{{else}}False{{/with}}'),
foo: {
isTruthy: true
}
});
runAppend(view);

equal(view.$().text(), 'True');

run(function() {
set(view, 'foo.isTruthy', false);
});

equal(view.$().text(), 'False');

run(function() {
set(view, 'foo.isTruthy', true);
});

equal(view.$().text(), 'True');
});

QUnit.test('should respect `isTruthy` field on the context object', function() {
view = EmberView.create({
template: compile('{{#with foo}}True{{else}}False{{/with}}'),
context: {
foo: {
isTruthy: true
}
}
});

runAppend(view);

equal(view.$().text(), 'True');

run(function() {
set(view, 'context.foo.isTruthy', false);
});

equal(view.$().text(), 'False');

run(function() {
set(view, 'context.foo.isTruthy', true);
});

equal(view.$().text(), 'True');
});

QUnit.test('the scoped variable is not available outside the {{with}} block.', function() {
view = EmberView.create({
template: compile('{{name}}-{{#with other as |name|}}{{name}}{{/with}}-{{name}}'),
Expand Down