From aa976283db244f3eb0761651db3c74c50a85f4dd Mon Sep 17 00:00:00 2001 From: Alan Johnson Date: Sat, 20 Nov 2010 17:08:06 -0500 Subject: [PATCH] Just two more failing tests on the new constext object rewrite. --- lib/handlebars.js | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/handlebars.js b/lib/handlebars.js index 34cae06a..d466d15a 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -22,7 +22,7 @@ var Handlebars = { var compiler = new Handlebars.Compiler(string); compiler.compile(); - return "context = new Handlebars.Context(context, fallback); var stack = stack || [];" + compiler.fn; + return "var stack = stack || [];" + compiler.fn; }, isFunction: function(fn) { @@ -106,13 +106,14 @@ var Handlebars = { buildContext: function(context, stack) { var ContextWrapper = function(stack) { + this.__context__ = context; this.__stack__ = stack.slice(0); this.__get__ = function(path) { - return Handlebars.evalExpression(path, this, this.__stack__); + return this.__context__.evalExpression(path, this.__stack__).data; }; }; - ContextWrapper.prototype = context; + ContextWrapper.prototype = context.data; return new ContextWrapper(stack); }, @@ -189,13 +190,13 @@ var Handlebars = { var out = "", args; originalArgs = arg.length ? arg : [null] - if (Handlebars.isFunction(lookup)) { + if (Handlebars.isFunction(lookup.data)) { args = originalArgs.concat(fn); - out = out + lookup.apply(context, args); + out = out + lookup.data.apply(context, args); - if (notFn != null && Handlebars.isFunction(lookup.not)) { + if (notFn != null && Handlebars.isFunction(lookup.data.not)) { args = originalArgs.concat(notFn); - out = out + lookup.not.apply(context, args); + out = out + lookup.data.not.apply(context, args); } } else { @@ -248,7 +249,7 @@ Handlebars.Context = function(context, fallback, path) { this.fallback = context.fallback; this.path = context.path; } else { - this.data = Handlebars.isEmpty(context) ? {} : context; + this.data = context; this.fallback = fallback || {}; this.path = path || ""; } @@ -257,7 +258,7 @@ Handlebars.Context.prototype = { // path: The path to evaluate // stack: The stack to work with evalExpression: function(path, stack) { - newContext = new Handlebars.Context(this); + var newContext = new Handlebars.Context(this); console.log("--- New context:") console.log(newContext); @@ -267,10 +268,10 @@ Handlebars.Context.prototype = { if (depth > stack.length) { newContext.data = null; } else if (depth > 0) { - newContext.data = stack[stack.length - depth]; + newContext = new Handlebars.Context(stack[stack.length - depth]); } - for (var i = 0; i < parts.length && typeof this.data !== "undefined" && this.data != null ; i++) { + for (var i = 0, j = parts.length; i < j && typeof this.data !== "undefined" && this.data !== null ; i++) { newContext.data = newContext.data[parts[i]]; } @@ -289,7 +290,7 @@ Handlebars.Compiler = function(string) { this.pointer = -1; this.mustache = false; this.text = ""; - this.fn = "var out = ''; var lookup; "; + this.fn = "context = new Handlebars.Context(context, fallback); var out = ''; var lookup; "; this.newlines = ""; this.comment = false; this.escaped = true; @@ -312,19 +313,20 @@ Handlebars.SafeString.prototype.toString = function() { } Handlebars.helperMissing = function(object, fn) { + console.log("HELPER MISSING"); var ret = ""; - if(object === true) { + if(object.data === true) { return fn(this); - } else if(object === false) { + } else if(object.data === false) { return ""; - } else if(Object.prototype.toString.call(object) === "[object Array]") { - for(var i=0, j=object.length; i