Skip to content

Commit

Permalink
Fix track id handling in partials
Browse files Browse the repository at this point in the history
Fixes #914
  • Loading branch information
kpdecker committed Aug 3, 2015
1 parent 9f265b9 commit 1c08771
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down
44 changes: 44 additions & 0 deletions spec/track-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit 1c08771

Please sign in to comment.