Skip to content

Commit

Permalink
Merge pull request #635 from sethkinast/medallia
Browse files Browse the repository at this point in the history
Resolve dynamic partial names via original context
  • Loading branch information
prashn64 committed Apr 17, 2015
2 parents 0120016 + e88ceb8 commit 216efee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
partial: function(context, node) {
return '.p(' +
compiler.compileNode(context, node[1]) +
',' + compiler.compileNode(context, node[2]) +
',ctx,' + compiler.compileNode(context, node[2]) +
',' + compiler.compileNode(context, node[3]) + ')';
},

Expand Down
18 changes: 9 additions & 9 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,26 +841,26 @@
return this;
};

Chunk.prototype.partial = function(elem, context, params) {
Chunk.prototype.partial = function(elem, context, partialContext, params) {
var head;

if (!dust.isEmptyObject(params)) {
context = context.clone();
head = context.pop();
context = context.push(params)
.push(head);
partialContext = partialContext.clone();
head = partialContext.pop();
partialContext = partialContext.push(params)
.push(head);
}

if (dust.isTemplateFn(elem)) {
// The eventual result of evaluating `elem` is a partial name
// Load the partial after getting its name and end the async chunk
return this.capture(elem, context, function(name, chunk) {
context.templateName = name;
load(name, chunk, context).end();
partialContext.templateName = name;
load(name, chunk, partialContext).end();
});
} else {
context.templateName = elem;
return load(elem, this, context);
partialContext.templateName = elem;
return load(elem, this, partialContext);
}
};

Expand Down
18 changes: 16 additions & 2 deletions test/jasmine-test/spec/coreTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,20 @@ var coreTests = [
expected: "Hello Joe! You have 30 new messages.",
message: "should test partial with literal inline param and context. Fallback values for name or count are undefined"
},
{
name: "partial with dynamic name and context",
source: '{>"{partialName}":me /}',
context: { partialName: "partial", me: { name: "Mick", count: 30 }},
expected: "Hello Mick! You have 30 new messages.",
message: "should test partial with dynamic name and a context"
},
{
name: "partial with dynamic name and context and inline params",
source: '{>"{partialName}" name=me.name count=me.count /}',
context: { partialName: "partial", me: { name: "Mick", count: 30 }},
expected: "Hello Mick! You have 30 new messages.",
message: "should test partial with dynamic name and a context"
},
{
name: "partial with blocks and inline params",
source: '{>partial_with_blocks name=n count="{c}"/}',
Expand Down Expand Up @@ -1490,8 +1504,8 @@ var coreTests = [
source: '{#helper template="partial"}{/helper}',
context: { "helper": function(chunk, context, bodies, params)
{
var newContext = {};
return chunk.partial(params.template, dust.makeBase(newContext));
var newContext = dust.makeBase({});
return chunk.partial(params.template, newContext, newContext, {});
}
},
expected: "Hello ! You have new messages.",
Expand Down

0 comments on commit 216efee

Please sign in to comment.