Throw errors in {{foo bar}}, foo not a function

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).
This commit is contained in:
David Greenspan
2014-07-15 14:20:27 -07:00
parent 2a382cfb8c
commit b5cbca980e
2 changed files with 37 additions and 5 deletions

View File

@@ -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);
};
}

View File

@@ -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) {