javascript - Dust.js 逻辑助手的问题

标签 javascript jquery asp.net-mvc json dust.js

我正在使用 jQuery 1.8.2Dust.js v1.1.1 在 JavaScript 应用程序中实现 MVC 样式的模板。当我使用 {@gt} 逻辑助手时,我收到以下控制台错误:

Uncaught TypeError: Cannot read property 'gt' of undefined

我相信我的模板中使用了正确的语法:

<ul class="listview">
{#seasons}
    <li>
        <h3>{name}</h3>
        <p class="desc">{id}</p>
        {@gt key="months" value="0"}
        <span class="count">{months}</span>
        {/gt}
    </li>
{/seasons}
</ul><!--// end .listview -->

这是 JSON 结构:

{
    "seasons":[
        {
            "name":"Spring",
            "id":"weklv7",
            "months": 8
        },
        {
            "name":"Summer",
            "id":"lvuec5",
            "months": 4
        }
    ]
}

如果我从模板中删除 {@gt} 逻辑助手,错误就会消失,并且模板会作为 HTML 正确加载。例如:

<ul class="listview">
{#seasons}
    <li>
        <h3>{name}</h3>
        <p class="desc">{id}</p>
        <span class="count">{months}</span>
    </li>
{/seasons}
</ul><!--// end .listview -->

非常感谢任何帮助,谢谢!

最佳答案

请密切关注提供的链接 smfoote。

为了在节点中“加载”灰尘助手,您需要的特定语法是:

var dust = require('dustjs-linkedin');
dust.helper = require('dustjs-helpers');

请注意,这不是链接上写的内容,链接文档表明这应该是带有“s”的 dust.helper(S)。

您根本不会在主要文档站点的任何地方找到该短语。

显然,这应该是连接事物的方式,这应该是完全直观的。

有时,我觉得那些未能记录他们对项目所做的重大更改的影响的人正在偷走我的生活。这个特殊的文档差距偷走了大约 5 个小时。

为了帮助那些同时运行 npm install dustjs-linkedin 和 npm install dustjs-helpers 并且想知道为什么它不能自动工作的人(因为它基本上是网站上所有文档所建议的) .当你在这里遇到这个错误时,我将重建你通常会遇到的错误:

  if( dust.helpers[name]){
                  ^

然后其中之一..

TypeError: Cannot read property 'if' of undefined
TypeError: Cannot read property 'math' of undefined
TypeError: Cannot read property 'eq' of undefined
TypeError: Cannot read property 'not eq' of undefined
TypeError: Cannot read property 'ne' of undefined
TypeError: Cannot read property 'lt' of undefined
TypeError: Cannot read property 'gt' of undefined
TypeError: Cannot read property 'select' of undefined
TypeError: Cannot read property 'size' of undefined
TypeError: Cannot read property 'tap' of undefined
TypeError: Cannot read property 'contextDump' of undefined
TypeError: Cannot read property 'idx' of undefined
TypeError: Cannot read property 'sep' of undefined

有趣的是,如果您将 dustjs-helpers 分配给“helpers”而不是“helper”,您会得到一个不同的错误:

undefined:1
").write("</form>").write("\n\n").helper("contextDump",ctx,{},null).write("\n\
                                                                    ^
TypeError: Cannot call method 'write' of undefined

编写一些代码来检测何时有人试图使用以“@”开头的东西并说..嘿你可能没有加载到你的帮助程序库......并使其在节点中工作是相当微不足道的和非节点上下文......或者他们可以修复文档。

我真诚地希望,我花额外的 15 分钟来拼凑一个 Google 可能会 Grok 的答案,可以为其他人节省我刚刚失去的 5 个小时。

-英尺

关于javascript - Dust.js 逻辑助手的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13326570/

相关文章:

jquery - Uncaught ReferenceError : $ is not defined after applying Bootstrap Template

javascript - 如何在 jQuery 中绕 z 轴旋转图像?

javascript - 带有子项的数组 - 以父子层次结构的形式显示它

jquery - 将 JQuery UI 选项卡与底部对齐

javascript - 避免 DOM 元素在 .text() 上被删除的方法

javascript - 使用 MVC 5 的 IE11 缓存问题

c# - 如何将枚举转换为 List<SelectListItem>?

javascript - react : Convert HOC const to component

javascript - 旋转木马 - Twitter BootStrap;隐藏上一个或下一个按钮

javascript - 通过设置 width=0 隐藏容器,但有时容器内的一些元素在容器隐藏后会短暂地保持可见