javascript - Dust.js 模板不遵守条件存在

标签 javascript json dust.js

我遇到了一个有趣的问题,我的 Dust.js 模板不遵守存在条件。

我的数据如下所示:

{"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
    }
  ],
}

模板(不起作用):

{?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}

问题是,无论发生什么,模板都只会显示 else 条件中的所有内容,除非在存在中我在guaranteeHistory 中输出一个值...

模板(有效):

{?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}

我找不到任何方法来解决这个问题。我也无法在dustjs.com 的开发模式下重复此问题,它在那里工作正常。任何人都可以根据提供的信息帮助或告诉我可能出现什么问题吗?

最佳答案

没有理由不能像您所描述的那样正常工作。另外,您提供的编译后的body_0绝对是正确的。

因此,如果这种情况真的发生,唯一可能的原因是:

  1. 您没有渲染您认为的模板。如果您自己编译和渲染,这似乎不太可能。
  2. guaranteedHistory 的值并不是您想象的那样。因为您正在 http://www.dustjs.com/test/test.html 上测试您的模板它在那里工作正常,这是我的猜测。

因此,这向我表明您存在竞争条件,并且您尝试在设置之前使用 guaranteedHistory 进行渲染(通过某些回调、Promise 等)。

灰尘2.7.2 doesn't resolve Promises in exists blocks所以这可能就是你所遇到的。如果需要,您可以使用 trunk Tip,直到 2.8 发布为止。

关于javascript - Dust.js 模板不遵守条件存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46245618/

相关文章:

ruby-on-rails - rails : Restrict API requests to JSON format

javascript - Jquery post 循环遍历 json 数据

javascript - dust js 循环遍历 json 中的对象

javascript - 为什么 node.js 没有发现我的错误?

javascript - 防止 PHP 在新页面中显示结果

javascript - 爬取 JSON-LD 架构 : detailedDescription undefined?

javascript - Dust.js Promise 数组

javascript - body hasclass 不起作用

c# - 如何使用 JSON.Net 将 System.Drawing.Size 反序列化/序列化为对象?

javascript - reactjs 和 dustjs 之间的概念差异