Reputation: 440
Why is it that when showOrders gets called the orders array is empty? My service is returning 4 objects and I can walk through the item initialization in the .push call but the array returned is always empty.
var showOrders = function(orders){
$.each(orders, function(i, val){
alert(val.Order + "::" + val.Href);
});
}
var orders = (function(d){
var _r = [];
$.getJSON("/_vti_bin/listdata.svc/Orders", function (data) {
for(var i = 0; i < data.d.results.length; i++)
{
var o = data.d.results[i];
_r.push({
Id: o.Id,
Order: o.Order,
PurchaseDate: o.PurchaseDate,
CustomerPO: o.CustomerPO,
PurchasedBy: o.PurchasedBy,
SubTotal: o.SubTotal,
Status: o.Status,
Href: o.Path + "/DispForm.aspx?ID=" + o.Id
});
}
return _r;
});
d(_r);
})(showOrders);
Upvotes: 1
Views: 227
Reputation: 14434
$.getJSON is excecuting an async call. you need to excecute the d-callback inside the callback of the getJSON-call (after your loop):
var orders = (function(d){
var _r = [];
$.getJSON("/_vti_bin/listdata.svc/Orders", function (data) {
for(var i = 0; i < data.d.results.length; i++)
{
var o = data.d.results[i];
_r.push({
Id: o.Id,
Order: o.Order,
PurchaseDate: o.PurchaseDate,
CustomerPO: o.CustomerPO,
PurchasedBy: o.PurchasedBy,
SubTotal: o.SubTotal,
Status: o.Status,
Href: o.Path + "/DispForm.aspx?ID=" + o.Id
});
}
d(_r);
});
})(showOrders);
Upvotes: 3