More work on context object. Just one more failing test.
This commit is contained in:
+4
-28
@@ -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
@@ -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++) {
|
||||
|
||||
Reference in New Issue
Block a user