javascript - Object scope. Why does this give a null object -


this question has answer here:

     initialize: function () {         var store = {};         var item = {};         var me = this;          ext.ajax.request({             url: "some_valid_url",             success: function (response) {                 try {                     var parser = new domparser();                     var xml = parser.parsefromstring(response.responsetext, "text/xml");                      store = ext.create('ext.data.store', {                         autoload: true,                         fields: ['temp', 'low', 'high', 'desc', 'icon'],                         data: xml,                         proxy: {                             type: 'memory',                             reader: {                                 type: 'xml',                                 rootproperty: 'current',                                 record: 'day'                             }                         }                     });                     item = ext.create("ext.container", {                       var bla = "hello world",                     })               }         });         console.log("store , item")         console.log(item);         console.log(store); 

why item , store give null objects? can see it's parsing data, if put console.log in between store , item elements valid element...

 store , item  object {} articleweatherlist.js:105 object {} articleweatherlist.js:106 

ajax asynchronous means code continues executing without waiting request complete. therefore, when trying use item , store, request has not yet completed , success method sets them has not yet run. ways round force request happen synchronously (although bad idea) or put code depends on request success callback or functions called within success callback.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -