简体   繁体   中英

Why am I not able to populate my Array object?

if (typeof response == "object") {
    store = new dojo.data.ItemFileReadStore({
        data : response
    });
    console.warn("Loaded to Store");
    var itemArray = new Array();
    var completed = function(items) {
        for(var i = 0; i < items.length; i++) {
            console.log(store.getValue(items[i],"itemlabel"));
            itemArray.push(store.getValue(items[i]));
        }
    };
    var error = function() {
        console.log("Error in fetching data from Store");
    };

    store.fetch({onComplete: completed, onError: error});
    console.warn("Item count"+ itemArray.length);

So my Item Count gives Always 0 but

console.log(store.getValue(items[i],"itemlabel"));

In the callback method the above get print the value.

So if i want to populate my itemArray what can i do?

You're printing itemArray.length before the array gets populated with any data in the completed function, because completed will be called asynchronously.

If you want to print the actual length of itemArray , you should do it after the for-loop in completed .

Hello the answer to your problem is quite easy, but first i'd recommand using

var completed = function(items) {
    dojo.forEach(items, function(item) {
        console.log(store.getValue(items[i],"itemlabel"));
        itemArray.push(store.getValue(items[i]));
    }, this);
    proceedToNextStep(itemArray);
};

and then you create a function that will be used to keep on the process once the itemArray is loaded..

proceedToNextStep: function(itemArray){
    // whatever you need to do
},

You could also use dojo.publish / dojo.subscribe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM