javascript - How to handle async getJSON to copy json objects? -


this question has answer here:

how assign/clone received json object defines javascript object booksjson ? need make booksjson accessible , visible functions.

var booksjson = {};     function getbooks(){     $.getjson( "bookstore.json", function( json ) {          $.each(json.books, function(i, json){             renderentity(json);         });         booksjson  =  json;  // clone objects     }); } 

update:

new code:

    var booksjson = {}; function getbooks(){     return $.getjson( "bookstore.json" ); }   $(document).ready(function(){       getbooks();      getbooks().done(function(json) {         $.each(json.books, function(i, json){             renderentity(json);         });         booksjson = json;         alert(json.stringify(booksjson));     });  }); 

it has nothing cloning! $.getjson asynchronous , can't use data before it's available once $.getjson function has completed, , that's success callback there for.

var booksjson = {}; function getbooks(){     $.getjson( "bookstore.json", function( json ) {          /* async function, executes @ later time */          $.each(json.books, function(i, json){             renderentity(json);         });         booksjson  =  json;  // here json available    });    console.log( booksjson ); // happens before above function,                               // here booksjson still empty object,                               // nothing has been added yet } 

the thing can :

function getbooks(){     return $.getjson( "bookstore.json" ); }  getbooks().done(function(json) {     $.each(json.books, function(i, json){         renderentity(json);     }); }); 

if helps ?


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -