Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

Commit

Permalink
Bind functions to the context properly.
Browse files Browse the repository at this point in the history
The regression was introduced in 1.0.rc.1.

This fixes issue handlebars-lang#317.
  • Loading branch information
Tyson Tate committed Sep 19, 2012
1 parent b5074a8 commit 42120d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/handlebars/compiler/compiler.js
Expand Up @@ -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;
});
},

Expand Down Expand Up @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions spec/qunit_spec.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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");

Expand All @@ -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", {
Expand Down

0 comments on commit 42120d1

Please sign in to comment.