diff --git a/packages/ember-htmlbars/lib/hooks/link-render-node.js b/packages/ember-htmlbars/lib/hooks/link-render-node.js index 68328ca1b2b..9c19a8867a0 100644 --- a/packages/ember-htmlbars/lib/hooks/link-render-node.js +++ b/packages/ember-htmlbars/lib/hooks/link-render-node.js @@ -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; } } @@ -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'); @@ -93,7 +94,7 @@ function shouldDisplay(predicate) { return isTruthyVal; } - return !!predicateVal; + return coercer(predicateVal); }, 'ShouldDisplay'); addDependency(stream, length); @@ -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); diff --git a/packages/ember-htmlbars/tests/helpers/with_test.js b/packages/ember-htmlbars/tests/helpers/with_test.js index fa564346655..00cf562f08f 100644 --- a/packages/ember-htmlbars/tests/helpers/with_test.js +++ b/packages/ember-htmlbars/tests/helpers/with_test.js @@ -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}}'),