1

After a JSONP call I am returned:

[
    {
        "text": "yo whats up?",
        "id": 1
    },
    {
        "text": "hey man!",
        "id": 2
    },
    {
        "text": "dude.",
        "id": 3
    }
]

Heres the actual API call:
http://api.twitter.com/1/statuses/user_timeline/codinghorror.json

Using Dust.js I would do something like:

<ul>
{#myPosts}
  <li>{text}, {id}{~n}</li>
{:else}
  <p>Humm...</p>
{/myPosts}
</ul>

But there is no key "myPosts" in the response. That's the problem. Checkout the API call too see how the JSON is being returned, mabye I'm interpreting this wrong.

What syntax would I use in Dust.js to iterate through each object in this array?

2 Answers 2

5

You can use the "current context" shortcut.

{#.}
  {text} - {id}<br />
{/.}
Sign up to request clarification or add additional context in comments.

1 Comment

I searched around dustjs, but couldn't find anything about #.. Where is this documented?
1

From dustjs {guide}, I would say, your example should already loop through the array. You just need it as

{
    myPosts: [
        {
            "text": "yo whats up?",
            "id": 1
        },
        {
            "text": "hey man!",
            "id": 2
        },
        {
            "text": "dude.",
            "id": 3
        }
    ]
}

From the Dust Tutorial - Dust under the covers, I've built this JSFiddle. Ignoring the boilerplate, it comes down to prefixing the received JSONP array with myPosts yourself and pass that to dustjs

var jsonp = [...];
dust.render('template', { myPosts: jsonp }, function (err, out) {
...
});

To access anything in your template, you must name it. There is no other way, AFAICS.

3 Comments

Yea but there is no key "myPosts". That's the problem. Checkout the API call too see how the JSON is being returned, mabye I'm interpreting this wrong.
@DanKanze I'm still looking for a solution. Downvoting doesn't speed that up. Good bye.
Yea I went through those docs and couldn't find anything either. It seems like this should be a request feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.