Skip to content

Commit

Permalink
Merge pull request #125 from sethkinast/selectState
Browse files Browse the repository at this point in the history
Improvements to checking for the existence of a {@select} state.
  • Loading branch information
prashn64 committed Mar 24, 2015
2 parents 15df891 + 57d9439 commit 762806a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function isSelect(context) {
}

function getSelectState(context) {
return context.get('__select__');
return isSelect(context) && context.get('__select__');
}

function addSelectState(context, key) {
Expand Down Expand Up @@ -75,7 +75,7 @@ function filter(chunk, context, bodies, params, filterOp) {
var body = bodies.block,
actualKey,
expectedValue,
selectState,
selectState = getSelectState(context),
filterOpType = params.filterOpType || '';

// Currently we first check for a key on the helper itself, then fall back to
Expand All @@ -84,8 +84,7 @@ function filter(chunk, context, bodies, params, filterOp) {
// it, just switch the order of the test below to check the {@select} first.)
if (params.hasOwnProperty("key")) {
actualKey = dust.helpers.tap(params.key, chunk, context);
} else if (isSelect(context)) {
selectState = getSelectState(context);
} else if (selectState) {
actualKey = selectState.key;
// Once one truth test in a select passes, short-circuit the rest of the tests
if (selectState.isResolved) {
Expand All @@ -98,7 +97,7 @@ function filter(chunk, context, bodies, params, filterOp) {
expectedValue = dust.helpers.tap(params.value, chunk, context);
// coerce both the actualKey and expectedValue to the same type for equality and non-equality compares
if (filterOp(coerce(expectedValue, params.type, context), coerce(actualKey, params.type, context))) {
if (isSelect(context)) {
if (selectState) {
if(filterOpType === 'default') {
selectState.isDefaulted = true;
}
Expand Down Expand Up @@ -476,12 +475,11 @@ var helpers = {
* The passing truth test can be before or after the {@any} block.
*/
"any": function(chunk, context, bodies, params) {
var selectState;
var selectState = getSelectState(context);

if(!isSelect(context)) {
if(!selectState) {
_log("{@any} used outside of a {@select} block", "WARN");
} else {
selectState = getSelectState(context);
if(selectState.isDeferredComplete) {
_log("{@any} nested inside {@any} or {@none} block. It needs its own {@select} block", "WARN");
} else {
Expand All @@ -505,12 +503,11 @@ var helpers = {
* The position of the helper does not matter.
*/
"none": function(chunk, context, bodies, params) {
var selectState;
var selectState = getSelectState(context);

if(!isSelect(context)) {
if(!selectState) {
_log("{@none} used outside of a {@select} block", "WARN");
} else {
selectState = getSelectState(context);
if(selectState.isDeferredComplete) {
_log("{@none} nested inside {@any} or {@none} block. It needs its own {@select} block", "WARN");
} else {
Expand Down
7 changes: 7 additions & 0 deletions test/jasmine-test/spec/helpersTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,13 @@
context: { foo: "bar", moo: "cow"},
expected: "Hello World",
message: "an any helper must have its own select to render"
},
{
name: "any with a multikey select",
source: '{@select key=one}{@eq value="true"/}{@eq key=two value="true"/}{@any}Hello{/any}{/select}',
context: { one: "false", two: "true" },
expected: "Hello",
message: "any helpers inside multikey selects render"
}
]
},
Expand Down

0 comments on commit 762806a

Please sign in to comment.