RayLoveless
RayLoveless

Reputation: 21068

Dustjs - Check condition regarding ALL items in an array with dustjs template

So I'm having trouble with a Dust.js template.

Suppose I have these example object models:

var lifeStory = [{
        "LifeEvent": "Birth",
        "year": "1963"
    }, 
    {
        "LifeEvent": "marriage",
        "year": "1963",
        "month": "Jul",
        "day": "15"
    }, 
    {
        "LifeEvent": "death"
    }
];

// or 

var lifeStory = [{
        "LifeEvent": "Birth"
    }, 
    {
        "LifeEvent": "Baptizm"
    }, 
    {
        "LifeEvent": "marriage"
    }, 
    {
        "LifeEvent": "death"
    }
];

and I have this dustjs template:

{#lifeStory}
<div class="myRow">
    <div class="DateColumn">        
        {year} - {month} - {day}
    </div>
    <div class="lifeEventColumn">
        {LifeEvent}
    </div>
</div>
{/lifeStory}

I want to hide the DateColumn if I don't have any year, month and day properties if my array. If only one item in the array has has date information I want to show the DateColumn for all rows.

I'm new to dust but am thinking this may require writing a custom dust helper. Any help would be great.

THANK YOU!!!!

Upvotes: 0

Views: 163

Answers (1)

Interrobang
Interrobang

Reputation: 17434

You want the {@any} helper. This helper will execute if any of the conditions you test are true.

We'll test for the presence of any of your date fields, and output the row if they exist.

Because Dust has some wonky coercion for equality checks, we'll cast everything to strings and check to see if they equal "undefined".

{#lifeStory}
<div class="myRow">
    {@select type="string"}
      {@ne key=year value="undefined"/}
      {@ne key=month value="undefined"/}
      {@ne key=day value="undefined"/}
      {@any}
        <div class="DateColumn">        
           {year} - {month} - {day}
        </div>
      {/any}
    {/select}
    <div class="lifeEventColumn">
        {LifeEvent}
    </div>
</div>
{/lifeStory}

Upvotes: 0

Related Questions