0
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",
                })
            } catch (err) {
                //err
            }
        }
    });
}

console.log("STORE AND ITEM");
console.log(item);
console.log(store);

Why item and store give back null objects?

However I can see that it's parsing some data, as if I put console.log in between store and item elements I will get a valid element.

STORE AND ITEM 
Object {} ArticleWeatherList.js:105
Object {} ArticleWeatherList.js:106
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Jackie Chan
  • 2,654
  • 6
  • 35
  • 70
  • so basiablly i need these items outside of the `ajax` request – Jackie Chan May 15 '13 at 10:34
  • 1
    Specifically in this case you need to restructure the logic for the asynchronicity: depending on what the overall picture is one solution is to add a callback function to the initialization function that is run when the AJAX call is finished. – JJJ May 15 '13 at 10:45

2 Answers2

1

Ajax is asynchronous which basically means that the code continues executing without waiting for the request to complete. Therefore, when you are trying to use item and store, the request has not yet completed and the success method which sets them has not yet run. The only ways round this are to force the request to happen synchronously (although this is generally a bad idea) or to put all the code that depends on the request into the success callback or functions that are called from within the success callback.

Rob Johnstone
  • 1,704
  • 9
  • 14
-2

Because, you have declared store and item as local variables inside intialize function. If you want access these variable outisde you must declare them in the global not in the function. The same can be achived omitting the var keywords in the declaration of the variables.

initialize: function () {
        store = {};
        item = {};
        var me = this;
        ....

this should works but is a considered a bad practice.

mcorte
  • 42
  • 2