Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble With Dust.js Logic Helpers

I'm working with jQuery 1.8.2 & Dust.js v1.1.1 for MVC-style templating within a JavaScript app. When I use the {@gt} logic helper I receive the following console error:

Uncaught TypeError: Cannot read property 'gt' of undefined

I believe the proper syntax is used in my template:

<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 -->

Here's the JSON structure:

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

If I remove the {@gt} logic helper from the template, the error disappears and the template is loaded correctly as HTML. For example:

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

Any help is much appreciated, thanks!

like image 363
Kevin Leary Avatar asked Dec 04 '22 13:12

Kevin Leary


2 Answers

Pay very very close attention to the link smfoote provided.

The specific syntax that you need in order to "load" the dust helpers in node is:

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

Note that this is -not- what is written on the link, the link documents that this should be dust.helper(S) with an 's'.

You will not find that phrase anywhere on the main documentation site at all.

Apparently, it should be entirely intuitive that this is the way things should be connected.

Sometimes I feel like people who fail to document the impact of significant changes that they make to their projects are stealing my life. This particular documentation gap stole about 5 hours.

In order to help those souls who have run both npm install dustjs-linkedin AND npm install dustjs-helpers and are wondering why that does not just automatically work (since that it is essentially what all of the documentation on the site suggests). I will reconstruct the errors that you will typically have when you get this bug here:

  if( dust.helpers[name]){
                  ^

and then one of..

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

Interestingly, if you assign the dustjs-helpers to "helpers" rather than "helper" you get a different error:

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

It is fairly trivial to write a little code to detect when someone is trying to use something that starts with "@" and say.. hey you problably have not loaded in your helper library... and make it work in node and not-node contexts... or they could just fix the documentation.

It is my sincere hope that the extra 15 minutes I spent to put together an answer that Google might Grok will save someone else the 5 hours that I just lost.

-ft

like image 156
ftrotter Avatar answered Dec 24 '22 00:12

ftrotter


It sounds like your problem is that dust.helpers doesn't exist, probably because it was removed from Dust core. Make sure you have Dust core and Dust helpers loaded wherever your templates are being rendered.

like image 26
smfoote Avatar answered Dec 23 '22 23:12

smfoote