Skip to content
Browse files

Bind functions to the context properly.

The regression was introduced in 1.0.rc.1.

This fixes issue #317.
  • Loading branch information...
1 parent b5074a8 commit 42120d1177cdb089c414e2b51ce441f95c53faad @tysontate tysontate committed Sep 19, 2012
Showing with 14 additions and 2 deletions.
  1. +2 −2 lib/handlebars/compiler/compiler.js
  2. +12 −0 spec/qunit_spec.js
View
4 lib/handlebars/compiler/compiler.js
@@ -642,7 +642,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.context.aliases.functionType = '"function"';
this.replaceStack(function(current) {
- return "typeof " + current + " === functionType ? " + current + "() : " + current;
+ return "typeof " + current + " === functionType ? " + current + ".apply(depth0) : " + current;
});
},
@@ -787,7 +787,7 @@ Handlebars.JavaScriptCompiler = function() {};
var nextStack = this.nextStack();
this.source.push('if (foundHelper) { ' + nextStack + ' = foundHelper.call(' + helper.callParams + '); }');
- this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '() : ' + nextStack + '; }');
+ this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.apply(depth0) : ' + nextStack + '; }');
},
// [invokePartial]
View
12 spec/qunit_spec.js
@@ -136,6 +136,8 @@ test("functions returning safestrings shouldn't be escaped", function() {
test("functions", function() {
shouldCompileTo("{{awesome}}", {awesome: function() { return "Awesome"; }}, "Awesome",
"functions are called and render their output");
+ shouldCompileTo("{{awesome}}", {awesome: function() { return this.more; }, more: "More awesome"}, "More awesome",
+ "functions are bound to the context");
});
test("paths with hyphens", function() {
@@ -616,6 +618,11 @@ test("Invert blocks work in knownHelpers only mode", function() {
var result = template({foo: false});
equal(result, "bar", "'bar' should === '" + result);
});
+test("Functions are bound to the context in knownHelpers only mode", function() {
+ var template = CompilerContext.compile("{{foo}}", {knownHelpersOnly: true});
+ var result = template({foo: function() { return this.bar; }, bar: 'bar'});
+ equal(result, "bar", "'bar' should === '" + result);
+});
suite("blockHelperMissing");
@@ -624,6 +631,11 @@ test("lambdas are resolved by blockHelperMissing, not handlebars proper", functi
var data = { truthy: function() { return true; } };
shouldCompileTo(string, data, "yep");
});
+test("lambdas resolved by blockHelperMissing are bound to the context", function() {
+ var string = "{{#truthy}}yep{{/truthy}}";
+ var boundData = { truthy: function() { return this.truthiness(); }, truthiness: function() { return false; } };
+ shouldCompileTo(string, boundData, "");
+});
var teardown;
suite("built-in helpers", {

0 comments on commit 42120d1

Please sign in to comment.
Something went wrong with that request. Please try again.