From b5cbca980e3459b3883ee3bdacddfca60d32ccd9 Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Tue, 15 Jul 2014 14:20:27 -0700 Subject: [PATCH] Throw errors in {{foo bar}}, foo not a function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix the “simpler helper” test and expand the cases where we detect that “foo” is missing or not a function (e.g. is a scalar property of the data context). --- packages/blaze/lookup.js | 13 ++++++++-- packages/spacebars-tests/template_tests.js | 29 +++++++++++++++++++--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/packages/blaze/lookup.js b/packages/blaze/lookup.js index e7ff5e15ee..31f5701fde 100644 --- a/packages/blaze/lookup.js +++ b/packages/blaze/lookup.js @@ -52,14 +52,23 @@ Blaze.View.prototype.lookup = function (name, _options) { return wrapHelper(bindToCurrentDataIfIsFunction(UI._globalHelpers[name])); } else { return function () { + var isCalledAsFunction = (arguments.length > 0); var data = Blaze.getCurrentData(); - if (lookupTemplate && ! (data && data[name])) + if (lookupTemplate && ! (data && data[name])) { throw new Error("No such template: " + name); + } + if (isCalledAsFunction && ! (data && data[name])) { + throw new Error("No such function: " + name); + } if (! data) return null; var x = data[name]; - if (typeof x !== 'function') + if (typeof x !== 'function') { + if (isCalledAsFunction) { + throw new Error("Can't call non-function: " + x); + } return x; + } return x.apply(data, arguments); }; } diff --git a/packages/spacebars-tests/template_tests.js b/packages/spacebars-tests/template_tests.js index 7305f40f61..6a789d5d52 100644 --- a/packages/spacebars-tests/template_tests.js +++ b/packages/spacebars-tests/template_tests.js @@ -39,17 +39,40 @@ Tinytest.add("spacebars-tests - template_tests - simple helper", function (test) tmpl.foo = 3; test.throws(function () { renderToDiv(tmpl); - }); + }, /Can't call non-function/); delete tmpl.foo; test.throws(function () { renderToDiv(tmpl); - }); + }, /No such function/); tmpl.foo = function () {}; // doesn't throw - var div = renderToDiv(tmpl); + div = renderToDiv(tmpl); test.equal(canonicalizeHtml(div.innerHTML), ''); + + // now "foo" is a function in the data context + delete tmpl.foo; + R.set(1); + div = renderToDiv(tmpl, { foo: function (x) { + return x + R.get(); + } }); + test.equal(canonicalizeHtml(div.innerHTML), "124"); + R.set(2); + Deps.flush(); + test.equal(canonicalizeHtml(div.innerHTML), "125"); + + test.throws(function () { + renderToDiv(tmpl, {foo: 3}); + }, /Can't call non-function/); + + test.throws(function () { + renderToDiv(tmpl, {foo: null}); + }, /No such function/); + + test.throws(function () { + renderToDiv(tmpl, {}); + }, /No such function/); }); Tinytest.add("spacebars-tests - template_tests - dynamic template", function (test) {