I like to pull in some remote data when rendering a ractive template. Due to the async nature of the request no data makes it into output. How can I make that happen?
var ractive = new Ractive({
debug: true,
el: 'container',
template: '#template',
data:
{
chapters:chapters,
load_text: function(path)
{
$.get(path, function( text ) {
return text;
});
}
}
});
A couple of solutions come to mind;
Create the Ractive object, make the request, and then call 'set' on the ractive object when you have data.
var ractive = new Ractive({
debug: true,
el: 'container',
template: '#template',
data: {
chapters: chapters,
load_text: ''
}
});
$.get(path, function( text ) {
ractive.set('load_text', text);
});
Alternatively, and this is not recommended, you could cause your request to become synchronous.
var ractive = new Ractive({
debug: true,
el: 'container',
template: '#template',
data: {
chapters: chapters,
load_text: function(path) {
$.ajax({
url: path,
success : function (text) {
return text;
},
async : false
});
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With