javascript - How use Mithril's m.request to load data from a server? -
i want store blocks of json file array. here current code, in controller (ctrl) :
var ctrl = this; var id = (location.href).replace(/.*\//g, ''); //use m.route() ? ctrl.list = []; m.request({method: "get", url: "/data/"+id}).then(function(blocks){ blocks.map(function(block) { ctrl.list.push(block); }); }); console.log(ctrl.list); //result : empty array. why ?
m.request
asynchronous operation: needs make request server, wait server answer, load contents, , give response — why implements then
: give callback can things data when arrives.
but console.log
happening after make request: response isn't ready yet. depends upon server data needs invoked inside then
callback function.
Comments
Post a Comment