繁体   English   中英

为什么我无法填充Array对象?

[英]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);

所以我的项目计数总是为0,但是

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

在上面的回调方法中,获取打印值。

所以,如果我想填充我的itemArrayitemArray怎么办?

您要在completed函数中填充任何数据之前打印itemArray.length ,因为completed将被异步调用。

如果要打印itemArray的实际长度, itemArray在for循环completed

您好,您问题的答案很简单,但首先我建议使用

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);
};

然后创建一个函数,该函数将在加载itemArray后继续进行处理。

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

您也可以使用dojo.publish / dojo.subscribe

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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