From f650b0d69ec70f82fc58022a530b95b0e4371f1d Mon Sep 17 00:00:00 2001 From: kpdecker Date: Wed, 15 Jan 2014 09:41:53 -0600 Subject: [PATCH] Expose the initial context via @root --- lib/handlebars/compiler/javascript-compiler.js | 2 +- lib/handlebars/runtime.js | 9 ++++++++- spec/data.js | 15 +++++++++++++++ spec/expected/empty.amd.js | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js index 213672497..d6baf13c5 100644 --- a/lib/handlebars/compiler/javascript-compiler.js +++ b/lib/handlebars/compiler/javascript-compiler.js @@ -121,7 +121,7 @@ JavaScriptCompiler.prototype = { var copies = "helpers = this.merge(helpers, " + namespace + ".helpers);"; if (this.environment.usePartial) { copies = copies + " partials = this.merge(partials, " + namespace + ".partials);"; } - if (this.options.data) { copies = copies + " data = data || {};"; } + if (this.options.data) { copies = copies + " data = this.initData(depth0, data);"; } out.push(copies); } else { out.push(''); diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index 5ae8d8a78..d31e5f4fe 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -1,6 +1,6 @@ module Utils from "./utils"; import Exception from "./exception"; -import { COMPILER_REVISION, REVISION_CHANGES } from "./base"; +import { COMPILER_REVISION, REVISION_CHANGES, createFrame } from "./base"; export function checkRevision(compilerInfo) { var compilerRevision = compilerInfo && compilerInfo[0] || 1, @@ -56,6 +56,13 @@ export function template(templateSpec, env) { } return programWrapper; }, + initData: function(context, data) { + data = data ? createFrame(data) : {}; + if (!('root' in data)) { + data.root = context; + } + return data; + }, merge: function(param, common) { var ret = param || common; diff --git a/spec/data.js b/spec/data.js index d84eae729..eab80ef36 100644 --- a/spec/data.js +++ b/spec/data.js @@ -236,4 +236,19 @@ describe('data', function() { equals("sad world?", result, "Overriden data output by helper"); }); + describe('@root', function() { + it('the root context can be looked up via @root', function() { + var template = CompilerContext.compile('{{@root.foo}}'); + var result = template({foo: 'hello'}, { data: {} }); + equals('hello', result); + + result = template({foo: 'hello'}, {}); + equals('hello', result); + }); + it('passed root values take priority', function() { + var template = CompilerContext.compile('{{@root.foo}}'); + var result = template({}, { data: {root: {foo: 'hello'} } }); + equals('hello', result); + }); + }); }); diff --git a/spec/expected/empty.amd.js b/spec/expected/empty.amd.js index 657970cd0..ca80b8850 100644 --- a/spec/expected/empty.amd.js +++ b/spec/expected/empty.amd.js @@ -2,7 +2,7 @@ define(['handlebars.runtime'], function(Handlebars) { Handlebars = Handlebars["default"]; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; return templates['empty'] = template(function (Handlebars,depth0,helpers,partials,data) { this.compilerInfo = [4,'>= 1.0.0']; -helpers = this.merge(helpers, Handlebars.helpers); data = data || {}; +helpers = this.merge(helpers, Handlebars.helpers); data = this.initData(depth0, data); var buffer = "";