Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save timmfin/639221 to your computer and use it in GitHub Desktop.
Save timmfin/639221 to your computer and use it in GitHub Desktop.
The context (both this and the passedContext) are set incorrectly for inverted sections. See the console output when you visit this page (requires firebug, webkit inspector, or something else that provides console.log).
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<script type="text/javascript" charset="utf-8" src="http://github.com/wycats/handlebars.js/raw/master/lib/handlebars.js"></script>
<script type="text/javascript" charset="utf-8">
var template = "Testing the context of inverted sections. {{#checkMe}} Yes {{/checkMe}} {{^checkMe}} No {{/checkMe}}";
function View(id) {
this.id = id;
this.checkMe = function(passedContext) {
console.log('context in checkMe is ', this, this.id, "passed context is", passedContext);
return true;
};
}
var template = Handlebars.compile(template);
template(new View('first-view'));
template(new View('second-view'));
</script>
</head>
<body>
<h4>Expected output</h4>
<code>
<pre>
context in checkMe is View... first-view passed context is View...
context in checkMe is View... first-view passed context is View...
context in checkMe is View... second-view passed context is View...
context in checkMe is View... second-view passed context is View...
</pre>
</code>
<h4>However, this is what I end up seeing when I run it</h4>
<code>
<pre>
context in checkMe is View... first-view passed context is View...
context in checkMe is DOMWindow... undefined passed context is undefined
context in checkMe is View... second-view passed context is View...
context in checkMe is DOMWindow... undefined passed context is undefined
</pre>
</code>
<h4>Go ahead and check the console to see what you got.</h4>
</body>
<!-- Note, here's what I get as the compiled function body: -->
<script type="text/javascript" charset="utf-8">
function func() {
// Begin captured compiled template
fallback = fallback || {};
var stack = [];
var out = '';
var lookup;
out = out + "Testing the context of inverted sections. ";
var wrappedContext = Handlebars.buildContext(context, stack);
stack.push(context);
var fn70 = function(context) {
var out = '';
var lookup;
out = out + " Yes ";
return out;
};
lookup = (Handlebars.evalExpression('checkMe', context, stack, fallback));
var fn70Not = null;
out = out + Handlebars.handleBlock(lookup, wrappedContext, context, fn70, fn70Not);
stack.pop();
out = out + " ";
var fn99 = function(context) {
var out = '';
var lookup;
out = out + " No ";
return out;
};
lookup = (Handlebars.evalExpression('checkMe', context, stack, fallback));
out = out + Handlebars.handleInvertedSection(lookup, context, fn99);
return out;
// End captured compiled template
};
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment