More work on context object. Just one more failing test.

This commit is contained in:
Alan Johnson
2010-11-20 21:40:35 -05:00
parent aa976283db
commit 4b4c13f40d
2 changed files with 6 additions and 29 deletions
+4 -28
View File
@@ -80,30 +80,6 @@ var Handlebars = {
return compiled;
},
// TODO: Delete me, since I'm in Handlebars.Context now!
evalExpression: function(path, context, stack, fallback) {
fallback = fallback || {};
var parsedPath = Handlebars.parsePath(path);
var depth = parsedPath[0];
var parts = parsedPath[1];
if (depth > stack.length) {
context = null;
} else if (depth > 0) {
context = stack[stack.length - depth];
}
for (var i = 0; i < parts.length && typeof context !== "undefined" && context != null ; i++) {
context = context[parts[i]];
}
if (parts.length == 1 && typeof context === "undefined") {
return fallback[parts[0]];
}
return context;
},
buildContext: function(context, stack) {
var ContextWrapper = function(stack) {
this.__context__ = context;
@@ -259,7 +235,7 @@ Handlebars.Context.prototype = {
// stack: The stack to work with
evalExpression: function(path, stack) {
var newContext = new Handlebars.Context(this);
console.log("--- New context:")
console.log("--- New context:");
console.log(newContext);
var parsedPath = Handlebars.parsePath(path);
@@ -271,7 +247,7 @@ Handlebars.Context.prototype = {
newContext = new Handlebars.Context(stack[stack.length - depth]);
}
for (var i = 0, j = parts.length; i < j && typeof this.data !== "undefined" && this.data !== null ; i++) {
for (var i = 0, j = parts.length; i < j && typeof newContext.data !== "undefined" && newContext.data !== null ; i++) {
newContext.data = newContext.data[parts[i]];
}
@@ -474,8 +450,8 @@ Handlebars.Compiler.prototype = {
addPartial: function(mustache, param) {
// either used a cached copy of the partial or compile a new one
this.fn += "if (typeof fallback['partials'] === 'undefined' || typeof fallback['partials']['" + mustache + "'] === 'undefined') throw new Handlebars.Exception('Attempted to render undefined partial: " + mustache + "');";
this.fn += "out = out + Handlebars.compilePartial(fallback['partials']['" + mustache + "'])(" + param + ", fallback, stack);";
this.fn += "console.log(context); if (typeof context.fallback['partials'] === 'undefined' || typeof context.fallback['partials']['" + mustache + "'] === 'undefined') throw new Handlebars.Exception('Attempted to render undefined partial: " + mustache + "');";
this.fn += "out = out + Handlebars.compilePartial(context.fallback['partials']['" + mustache + "'])(" + param + ", stack);";
},
parseMustache: function() {
+2 -1
View File
@@ -292,13 +292,14 @@ test("nested block helpers", function() {
});
test("block inverted sections", function() {
shouldCompileTo("{{#people}}{{name}}{{^}}{{../none}}{{/people}}", {none: "No people"},
shouldCompileTo("{{#people}}{{name}}{{^}}{{../none}}{{/people}}", {none: "No people"},
"No people");
});
test("block helper inverted sections", function() {
var string = "{{#list people}}{{name}}{{^}}<em>Nobody's here</em>{{/list}}"
var list = function(context, fn) {
console.log(context);
if (context.length > 0) {
var out = "<ul>";
for(var i = 0,j=context.length; i < j; i++) {