Skip to content

Commit

Permalink
Correctly render more than one nested truth test
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Kinast committed Apr 27, 2015
1 parent 69d1173 commit 411abc7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function addSelectState(context, opts) {
}

var state = {
isPending: false,
isResolved: false,
isDeferredComplete: false,
deferreds: []
Expand Down Expand Up @@ -105,7 +106,7 @@ function filter(chunk, context, bodies, params, helperName, test) {
var body = bodies.block,
skip = bodies.else,
selectState = getSelectState(context) || {},
key, value, type;
willResolve, key, value, type;

// Once one truth test in a select passes, short-circuit the rest of the tests
if (selectState.isResolved) {
Expand All @@ -128,10 +129,16 @@ function filter(chunk, context, bodies, params, helperName, test) {
value = coerce(context.resolve(params.value), type);

if (test(key, value)) {
if(body) {
// Once a truth test passes, put the select into "pending" state. Now we can render the body of
// the truth test (which may contain truth tests) without altering the state of the select.
if (!selectState.isPending) {
willResolve = true;
selectState.isPending = true;
}
if (body) {
chunk = chunk.render(body, context);
}
if (selectState) {
if (willResolve) {
selectState.isResolved = true;
}
} else if (skip) {
Expand Down
3 changes: 2 additions & 1 deletion test/jasmine-test/spec/helpersTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,8 @@
name: "select with nested @eq",
source: ['{@select key=selectKey}',
'{@eq value=1}One',
'{@eq key=test value=5}Correct!{/eq}',
'{@eq key=test value=5}Correct{/eq}',
'{@eq key=test value=5}!{/eq}',
'{@select}',
'{@eq value=1}Bug! No key specified{:else}Bug! No key specified{/eq}',
'{@eq key=test value=5}InnerCorrect!{/eq}',
Expand Down

0 comments on commit 411abc7

Please sign in to comment.