Sta*_*ers 5 javascript ractivejs
我喜欢在渲染ractive模板时提取一些远程数据.由于请求的异步性质,没有数据使其成为输出.我怎么能做到这一点?
var ractive = new Ractive({
debug: true,
el: 'container',
template: '#template',
data:
{
chapters:chapters,
load_text: function(path)
{
$.get(path, function( text ) {
return text;
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
小智 13
我想到了几个解决方案;
创建Ractive对象,发出请求,然后在有数据时在ractive对象上调用'set'.
var ractive = new Ractive({
debug: true,
el: 'container',
template: '#template',
data: {
chapters: chapters,
load_text: ''
}
});
$.get(path, function( text ) {
ractive.set('load_text', text);
});
Run Code Online (Sandbox Code Playgroud)
或者,建议不要这样做,否则可能导致请求变为同步.
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
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3347 次 |
| 最近记录: |