Pass data from JS to mustache template

Pass data from JS to mustache template

by nest87 nest87 -
Number of replies: 2

I want to push some data to the mustache template which I retrieved via webservice. this data need to append under course card blocks. my service function fetching data by course id.

I tried this but didnt worked

     $(".card .dashboard-card[data-region=course-content]").closest('[data-course-id]').each(function() {

            var id= $(this).data('course-id');

            return Repository.getDataById(id)

                .then(function(data) {

              return Templates.render('block_my_blockname/templatename', courses)

                }).then(function(html, js) {

                    return Templates.replaceNodeContents(data, html, js);

                }).catch(Notification.exception);

        });


Average of ratings: -
In reply to nest87 nest87

Re: Pass data from JS to mustache template

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
The most obvious thing wrong with the code is that Repository.getDataById() calls an anonymous function with a parameter called 'data'. Within that function the value in 'data' is completely ignored, and a brand new variable called 'courses' (not previously mentioned in your code) is used to pass data into the template renderer.

At a guess, you meant to pass data in to the template renderer?

I notice that further down, you appear to be using the variable 'data' as the container that the HTML should be inserted into.

I could be misreading the code, so apologies if I've got this wrong.
In reply to Davo Smith

Re: Pass data from JS to mustache template

by nest87 nest87 -
I want to pass data which I fetched via webservice to my mustache template

That Repository function is written inside same js [repository.js] as below.

var getDataById= function(args) {
var request = {
methodname: 'block_myblock_webservice_name',
args: {
'id': args
}
};

var promise = Ajax.call([request])[0];
return promise;
};


view.js
--------
$(".card .dashboard-card[data-region=course-content]").closest('[data-course-id]').each(function() {

var id= $(this).data('course-id');

return Repository.getDataById(id)

.then(function(data) {

return Templates.render('block_my_blockname/templatename', data)

}).then(function(html, js) {

return Templates.replaceNodeContents('.teamplate_class_name', html, js);

}).catch(Notification.exception);

});