Dev
Dev

Reputation: 103

Marko and Koajs: Why is my async-fragment rendering not working?

I'm trying to get progressive rendering to work in Marko and Koajs.

First, I use the request-promise module to make a promise for a JSON data feed.

rp = require('request-promise')

function getDataFeed() {

    var url = "http://api.dronestre.am/data";
    var options = {
        url: url,
        headers: { 'User-Agent': 'request' },
    };

    rp(options).then(function(result) {

        var info = JSON.parse(result);
        return info.strike;
    });
};

Then I use the koa router and Marko to load a template where the feed goes in as data

// router method to serve first visualization
router.get('/first', function *() {

    let data = {
        strikes : getDataFeed()
    };

    this.body =  marko.load('./views/first.marko').stream(data);
    this.type = "text/html";
})

And finally, in my template, I use async-fragment to indicate that the section of the page should be asynchronously loaded:

<async-fragment data-provider='data.strikes' var='strikes' client-reorder='true'>
    <async-fragment-placeholder>
        <p>Loading...</p>
    </async-fragment-placeholder>
    <for each="strike in strikes">
        <tr>
            <td>${strike.number}</td>
            <td>${strike.country}</td>
            <td>${strike.date}</td>
            <td>${strike.narrative}</td>
            <td>${strike.town}</td>
            <td>${strike.deaths}</td>
            <td>${strike.target}</td>
            <td>${strike.lat}</td>
            <td>${strike.lon}</td>
        </tr>
    </for>  
</async-fragment>

Unfortunately, nothing ever loads in the async-fragment block. Not even the placeholder.

How do I get the placeholder to show and then the data to take its place in my template?

Upvotes: 1

Views: 560

Answers (1)

Dev
Dev

Reputation: 103

Problem solved! I needed to return the promise function in getDataFeed()

Solution:

rp = require('request-promise')

function getDataFeed() {

    var url = "http://api.dronestre.am/data";
    var options = {
        url: url,
        headers: { 'User-Agent': 'request' },
    };

   return rp(options).then(function(result) {

        var info = JSON.parse(result);
        return info.strike;
    });
};

Upvotes: 1

Related Questions