[英]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"));
在上面的回调方法中,获取打印值。
所以,如果我想填充我的itemArray
我itemArray
怎么办?
您要在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.