Dust.js逻辑助手的麻烦

时间:2012-11-10 22:14:02

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

我正在使用 jQuery 1.8.2 & Dust.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 -->

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

6 个答案:

答案 0 :(得分:7)

非常关注所提供的链接smfoote。

在节点中“加载”防尘助手所需的特定语法是:

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

请注意,这不是链接上写的内容,链接文件应该是dust.helper(S)和's'。

您根本无法在主文档网站的任何位置找到该短语。

显然,这应该是应该连接的方式。

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

为了帮助那些同时运行npm的灵魂安装dustjs-linkedin和npm安装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分配给“helper”而不是“helper”,你会得到一个不同的错误:

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

编写一些代码来检测某人是否正在尝试使用以“@”开头的内容并说...嘿,你可能没有加载到你的帮助库中...并使其在节点中工作,这是相当简单的和非节点上下文......或者他们可以修复文档。

我真诚地希望,我花了额外的15分钟来回答一下谷歌可能会让Grok在我失去的5个小时内拯救其他人的答案。

-ft

答案 1 :(得分:5)

听起来你的问题是dust.helpers不存在,可能是因为它was removed from Dust core。确保在模板呈现的任何位置都加载了Dust core并Dust helpers

答案 2 :(得分:3)

尽管ftrotter在这里提供了很好的信息,但我只想添加,因为我仍然迷失在“s”中。以下是编写自定义帮助程序时必须执行的操作:

dust.helper = require('dustjs-helpers');
dust.helpers.myHelper = function (chunk, ctx, bodies, params) {

当我需要时,请注意我没有“s”,但在编写帮助时必须添加“s”。

答案 3 :(得分:1)

要使其仅在客户端工作(例如,在仅客户端浏览器中运行以进行开发),在此工作演示页面上查看源代码有很多帮助: http://linkedin.github.com/dustjs/test/test.html?q=helpers

该网站对于调试语法也非常有用。

例如,select的示例不适用于客户端javascript编译。

{@select key=\"{foo}\"}
     {@eq value=\"bar\"}foobar{/eq}
     {@eq value=\"baz\"}foobaz{/eq}
     {@default} - default Text{/default}
 {/select}

但这样做:

 {@select key="{foo}"}
     {@eq value="bar"}foobar{/eq}
     {@eq value="baz"}foobaz{/eq}
     {@default} - default Text{/default}
 {/select}

很明显为什么改变有效,但是当你正在学习时,那个页面非常有帮助。

答案 4 :(得分:0)

如果你不能添加助手,这将是一个非常奇怪的决定,你仍然可以创建这样的自己的功能,可以用来代替 @gt或@math

var baseContext = dust.makeBase({
    position: function(chunk, context) {
        return context.stack.index + 1;
    },
  });

现在你可以使用{position}而不是$ {idx}来计算从1到n的循环。

答案 5 :(得分:0)

编辑:

自从我几天前写这篇文章以来,我学到了很多关于灰尘的知识。这是一些澄清!

dustjs-helpers库仅提供文档中列出的“预制”助手。 需要创建您自己的帮助程序。 require('dustjs-linkedin')可与require('dustjs-helpers')互换,具体取决于您是否需要默认帮助程序。 (在你的情况下,你这样做。)

另外,我在寻找帮助时发现了这一点,使得这一切都在Express中工作但我意识到你的问题与此无关。希望这项工作对于那些可能会像我一样偶然发现这个问题的人有用,但是,我会留在这里。


注意:dustjs-helpers需要在dustjs-linkedin中并返回它的一个实例,所以你真正需要做的只是var dust = require('dustjs-helpers');以获得一个正确的灰尘支持实例(与尘埃支持完全相同) - 使用过的)。你需要安装dustjs-linkedin才能使需求成功,但就是这样。

那就是说,我厌倦了争吵巩固,adaro,dustjs-linkedin,dustjs-helpers等等,没有一个以直接的方式在Express中使用这个功能,并写了一个小包装。

这是我的示例代码,添加了几个帮助程序。

https://gist.github.com/myndzi/8837944

我在我的应用设置中调用它:

var dust = require('./lib/dust-express');
app.engine('dust', dust(app, {
    publicdir: path.join(app.get('basedir'), 'public'),
    mtimes: app.util.mtimes,
    helpers: true
}));

希望这对某人有用。