From 1c08771215af7377fb8f33a26f64b3af0e06c168 Mon Sep 17 00:00:00 2001 From: kpdecker Date: Mon, 3 Aug 2015 12:16:02 -0500 Subject: Fix track id handling in partials Fixes #914 --- lib/handlebars/runtime.js | 6 ++++++ spec/track-ids.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index 5f73897..d41b42a 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -36,6 +36,9 @@ export function template(templateSpec, env) { function invokePartialWrapper(partial, context, options) { if (options.hash) { context = Utils.extend({}, context, options.hash); + if (options.ids) { + options.ids[0] = true; + } } partial = env.VM.resolvePartial.call(this, partial, context, options); @@ -193,6 +196,9 @@ export function resolvePartial(partial, context, options) { export function invokePartial(partial, context, options) { options.partial = true; + if (options.ids) { + options.data.contextPath = options.ids[0] || options.data.contextPath; + } if (partial === undefined) { throw new Exception('The partial ' + options.name + ' could not be found'); diff --git a/spec/track-ids.js b/spec/track-ids.js index 88db789..30a4661 100644 --- a/spec/track-ids.js +++ b/spec/track-ids.js @@ -190,4 +190,48 @@ describe('track ids', function() { }); }); }); + + describe('partials', function() { + var helpers = { + blockParams: function(name, options) { + return name + ':' + options.ids[0] + '\n'; + }, + wycats: function(name, options) { + return name + ':' + options.data.contextPath + '\n'; + } + }; + + it('should pass track id for basic partial', function() { + var template = CompilerContext.compile('Dudes: {{#dudes}}{{> dude}}{{/dudes}}', {trackIds: true}), + hash = {dudes: [{name: 'Yehuda', url: 'http://yehuda'}, {name: 'Alan', url: 'http://alan'}]}; + + var partials = { + dude: CompilerContext.compile('{{wycats name}}', {trackIds: true}) + }; + + equals(template(hash, {helpers: helpers, partials: partials}), 'Dudes: Yehuda:dudes.0\nAlan:dudes.1\n'); + }); + + it('should pass track id for context partial', function() { + var template = CompilerContext.compile('Dudes: {{> dude dudes}}', {trackIds: true}), + hash = {dudes: [{name: 'Yehuda', url: 'http://yehuda'}, {name: 'Alan', url: 'http://alan'}]}; + + var partials = { + dude: CompilerContext.compile('{{#each this}}{{wycats name}}{{/each}}', {trackIds: true}) + }; + + equals(template(hash, {helpers: helpers, partials: partials}), 'Dudes: Yehuda:dudes..0\nAlan:dudes..1\n'); + }); + + it('should invalidate context for partials with parameters', function() { + var template = CompilerContext.compile('Dudes: {{#dudes}}{{> dude . bar="foo"}}{{/dudes}}', {trackIds: true}), + hash = {dudes: [{name: 'Yehuda', url: 'http://yehuda'}, {name: 'Alan', url: 'http://alan'}]}; + + var partials = { + dude: CompilerContext.compile('{{wycats name}}', {trackIds: true}) + }; + + equals(template(hash, {helpers: helpers, partials: partials}), 'Dudes: Yehuda:true\nAlan:true\n'); + }); + }); }); -- cgit v1.1