Search code examples
javascriptjsondust.js

Dust.js Template Not Honoring Conditional Exists


I am running into an interesting issue where my Dust.js template is not honoring an exists conditional.

My data looks like this:

{"guaranteedHistory": [
    {
      "depositDate": "2017-08-31T00:00:00.000-0500",
      "interestRate": 515.5,
      "maturityDate": "2017-08-31T00:00:00.000-0500",
      "beginningBalance": 874757257.4,
      "deposits": 4.25,
      "transferIn": 75.7,
      "investmentReturn": 52.71,
      "amtReinvested": 5.5,
      "maturityWithdrawal": 6.66,
      "surrenderCharge": 7.77,
      "endingBalance": 8.88,
      "surrenderValue": 5735.56
    }
  ],
}

Template (Doesn't work):

{?guaranteedHistory}
  <table id="a">
    <thead>
      <tr>
        <th>&nbsp;</th>
        <th>What happened this period</th>
        <th></th>
      </tr>
    </thead>
  </table>
{:else}
  <table id="b">
    <thead>
      <tr>
        <th>Deposits</th>
        <th>Dividends</th>
        <th>Investment return</th>
      </tr>
    </thead>
  </table>
{/guaranteedHistory}

The issue is that no matter what happens, the template will only display everything in the else conditional UNLESS inside the exists I output a value inside guaranteedHistory...

Template (This works):

{?guaranteedHistory}
  {guaranteedHistory[0].depositDate}
  <table id="a">
    <thead>
      <tr>
        <th>&nbsp;</th>
        <th>What happened this period</th>
        <th></th>
      </tr>
    </thead>
  </table>
{:else}
  <table id="b">
    <thead>
      <tr>
        <th>Deposits</th>
        <th>Dividends</th>
        <th>Investment return</th>
      </tr>
    </thead>
  </table>
{/guaranteedHistory}

I was not able to find any way to get around this. I also could not duplicate this issue within a dev mode on dustjs.com, it works correctly there. Could anyone please help or tell me what could possibly be going wrong based on the provided information?


Solution

  • There is no reason that this shouldn't work correctly as you've described it. In addition, the compiled body_0 you provided is definitely correct.

    So, if this really is happening, the only possible reasons are:

    1. You aren't rendering the template you think you are. This seems unlikely if you're compiling and rendering yourself.
    2. The value of guaranteedHistory isn't what you think it is. Because you are testing your template on http://www.dustjs.com/test/test.html and it works properly there, this is my guess.

    So, this suggests to me that you have a race condition, and that you're trying to render with guaranteedHistory before it has been set (via some callback, Promise, etc).

    Dust 2.7.2 doesn't resolve Promises in exists blocks so this may be what you're hitting. You can use the trunk tip if you need to until 2.8 is released.