Html REST数据源绑定Listview中的嵌套REST调用
我有一个Windows8JavaScript应用程序,其中我将一个从RESTful服务检索产品的VirtualDataSource绑定到一个Listview //在用户滚动时获取数据 itemsFromIndex:函数requestIndex、countBefore、countAfter{Html REST数据源绑定Listview中的嵌套REST调用,html,rest,xmlhttprequest,windows-store-apps,winjs,Html,Rest,Xmlhttprequest,Windows Store Apps,Winjs,我有一个Windows8JavaScript应用程序,其中我将一个从RESTful服务检索产品的VirtualDataSource绑定到一个Listview //在用户滚动时获取数据 itemsFromIndex:函数requestIndex、countBefore、countAfter{ return WinJS.xhr(options).then( //Callback for success functi
return WinJS.xhr(options).then(
//Callback for success
function (request) {
var results = [], count;
console.log("request.responseText: " + request.responseText);
// Use the JSON parser on the results, safer than eval
var obj = JSON.parse(request.responseText);
console.log("obj: " + obj);
// Verify if the service has returned images
if (obj.length > 0) {
var items = obj;
console.log("returned items: " + items.length);
console.log("returned items: " +items[0].productid);
// Data adapter results needs an array of items of the shape:
// items =[{ key: key1, data : { field1: value, field2: value, ... }}, { key: key2, data : {...}}, ...];
// Form the array of results objects
for (var i = 0, itemsLength = items.length; i < itemsLength; i++) {
var dataItem = items[i];
var text = "aaa";
**var a = fetchPrice(dataItem.productid);**
console.log("shortText = " + a.text);
results.push({
key: (fetchIndex + i).toString(),
data: {
productId: dataItem.productid,
price: a
}
});
}
到目前为止还不错……麻烦的是……一旦我检索到所有产品,我需要通过调用另一个Web服务来显示产品的价格,但要在同一个listview模板中显示。
最好的方法是什么?正如您可能看到的,我已经添加了fetchPrice这样的代码,这是另一个WinJS.xhr调用。有更好的方法吗?我可以将price html元素绑定到productid,而productid只在运行时可用吗
请帮忙
谢谢,
托马斯
-
实际上现在有点进一步了:
var obj = JSON.parse(request.responseText);
console.log("obj: " + obj);
// Verify if the service has returned images
if (obj.length > 0) {
var items = obj;
console.log("returned items: " + items.length);
console.log("returned items: " + items[0].productid);
var priceText;
that.getPriceText(items[0].productid).done(function (b) { priceText = b; console.log("in function : " + priceText); });
console.log("priceText is " + priceText);
// console.log("var b = " + b.isPrototypeOf);
// Data adapter results needs an array of items of the shape:
// items =[{ key: key1, data : { field1: value, field2: value, ... }}, { key: key2, data : {...}}, ...];
// Form the array of results objects
for (var i = 0, itemsLength = items.length; i < itemsLength; i++) {
var dataItem = items[i];
results.push({
key: (fetchIndex + i).toString(),
data: {
productId: dataItem.productid,
price: priceText
}
});
}
第二行console.log console.logpriceTextis+priceText的控制台输出返回undefined:这是预期的,因为priceText异步调用尚未完成
异步调用的.done函数中的priceText输出正确输出到控制台。函数中的行读取:etc
如何将异步调用的值直接传递到将项目推送到返回数组的行中的price字段中?
我已尝试将价格设置为.done承诺返回值:
price:that.getPriceTextitems[0].productid.donefunction b{return b;}
然而,在我的WebApp中,price仍然是未定义的,我假设是因为第一个承诺返回所有已完成的项目并显示所有结果。
这实际上就是我想要的:显示所有产品详细信息,然后异步检索价格。
有人能提出实现这一目标的最佳方法吗
谢谢