diff options
author | Blake Embrey <hello@blakeembrey.com> | 2014-01-09 12:45:26 +1000 |
---|---|---|
committer | Blake Embrey <hello@blakeembrey.com> | 2014-01-16 21:24:26 +1000 |
commit | 13633e7896b9c104667102b3b5715fc1a6b0d613 (patch) | |
tree | 54131854d063b63225460a4c76022e1913ff9ec6 /lib/handlebars/compiler/javascript-compiler.js | |
parent | 5659db4877b7e28284bb7edb08d8c1d081d5726e (diff) | |
download | handlebars.js-13633e7896b9c104667102b3b5715fc1a6b0d613.zip handlebars.js-13633e7896b9c104667102b3b5715fc1a6b0d613.tar.gz handlebars.js-13633e7896b9c104667102b3b5715fc1a6b0d613.tar.bz2 |
Improve usefulness of extend util, properly use namespace property, update setup options to use a hash helper.
Diffstat (limited to 'lib/handlebars/compiler/javascript-compiler.js')
-rw-r--r-- | lib/handlebars/compiler/javascript-compiler.js | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js index abdc805..7898bd1 100644 --- a/lib/handlebars/compiler/javascript-compiler.js +++ b/lib/handlebars/compiler/javascript-compiler.js @@ -21,8 +21,7 @@ JavaScriptCompiler.prototype = { ret = parent + "[" + name + "]"; } else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) { ret = parent + "." + name; - } - else { + } else { ret = parent + "['" + name + "']"; } @@ -168,7 +167,7 @@ JavaScriptCompiler.prototype = { this.pushSource("return buffer;"); } - var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"]; + var params = this.isChild ? ["depth0", "data"] : [this.namespace, "depth0", "helpers", "partials", "data"]; for(var i=0, l=this.environment.depths.list.length; i<l; i++) { params.push("depth" + this.environment.depths.list[i]); @@ -819,6 +818,18 @@ JavaScriptCompiler.prototype = { .replace(/\u2029/g, '\\u2029') + '"'; }, + setupHash: function(obj) { + var pairs = []; + + for (var key in obj) { + if (obj.hasOwnProperty(key)) { + pairs.push(this.quotedString(key) + ':' + obj[key]); + } + } + + return '{' + pairs.join(',') + '}'; + }, + setupHelper: function(paramSize, name, blockHelper) { var params = [], paramsInit = this.setupParams(name, paramSize, params, blockHelper); @@ -833,14 +844,14 @@ JavaScriptCompiler.prototype = { }, setupOptions: function(helper, paramSize, params) { - var options = [], contexts = [], types = [], param, inverse, program; + var options = {}, contexts = [], types = [], param, inverse, program; - options.push("name:" + this.quotedString(helper)); - options.push("hash:" + this.popStack()); + options.name = this.quotedString(helper); + options.hash = this.popStack(); if (this.stringParams) { - options.push("hashTypes:" + this.popStack()); - options.push("hashContexts:" + this.popStack()); + options.hashTypes = this.popStack(); + options.hashContexts = this.popStack(); } inverse = this.popStack(); @@ -859,27 +870,27 @@ JavaScriptCompiler.prototype = { inverse = "self.noop"; } - options.push("inverse:" + inverse); - options.push("fn:" + program); + options.fn = program; + options.inverse = inverse; } - for(var i=0; i<paramSize; i++) { + for (var i = 0; i < paramSize; i++) { param = this.popStack(); params.push(param); - if(this.stringParams) { + if (this.stringParams) { types.push(this.popStack()); contexts.push(this.popStack()); } } if (this.stringParams) { - options.push("contexts:[" + contexts.join(",") + "]"); - options.push("types:[" + types.join(",") + "]"); + options.types = "[" + types.join(",") + "]"; + options.contexts = "[" + contexts.join(",") + "]"; } - if(this.options.data) { - options.push("data:data"); + if (this.options.data) { + options.data = "data"; } return options; @@ -888,7 +899,7 @@ JavaScriptCompiler.prototype = { // the params and contexts arguments are passed in arrays // to fill in setupParams: function(helperName, paramSize, params, useRegister) { - var options = '{' + this.setupOptions(helperName, paramSize, params).join(',') + '}'; + var options = this.setupHash(this.setupOptions(helperName, paramSize, params)); if (useRegister) { this.useRegister('options'); |