[英]How to get attr of element from ajax success result of codeigniter calendar?
[英]Get result from success handler
我正在我的應用程序中列出游戲。 有些是付費的,有些是免費的,所以在上市期間我正在調用一種方法來從 Play 商店獲取價格並在 Success 回調中獲得響應。 我的問題是我的成功處理程序在列出所有游戲后設置價格,因此所有游戲都列為免費。 我的代碼是
上市游戲
function render(res)
{
for (var i = 0; i < res.length; i++) {
var data=res[i];
if(data.product_id!='Free' && data.product_id!='')
{
getDetails(data.product_id);
}
else
{
price='Free';
}
console.log(price);
var gameOnScreen = "<li class='game-box showlist' data-tag='" + app_tag + "' >" +
"<div class='game-box-link' data-id='" + game.ID + "' data-orientation='" + orientation + "' data-ajax='false' data-url='" + link + "' data-transition='none'>" +
"<img src='" + thumb + "' class='game-thumb'>" +price+
"</div></li>";
}
}
}
獲取詳細信息
function getDetails(productlist)
{
inappbilling.getProductDetails(successHandler2, errorHandler, productlist);
}
成功處理程序2
function successHandler2 (result)
{
price= result[0].price;
console.log(price);
}
在圖像中,您可以看到列出所有游戲后的價格。
我只是將您的代碼重寫為遞歸調用:
function render(res)
{
var length = res.length;
var i=0;
var price;
function display(){
var gameOnScreen = "<li class='game-box showlist' data-tag='" + app_tag + "' >" +
"<div class='game-box-link' data-id='" + game.ID + "' data-orientation='" + orientation + "' data-ajax='false' data-url='" + link + "' data-transition='none'>" +
"<img src='" + thumb + "' class='game-thumb'>" +price+
"</div></li>";
i++;//incrementing to nextitem, but not triggering next iteration as a loop.
};
function getDetails()
{
if(res[i].product_id!='Free' && res[i].product_id!='')
{
inappbilling.getProductDetails(successHandler2, errorHandler, res[i].product_id);
}
else
{
price='Free';
display();//displaying item
}
};
function successHandler2 (result)
{
price= result[0].price;
console.log(price);
display();//displaying current item
if(i<length){
getDetails();//after getting current item's price go for next
}
};
getDetails();//initial call
}
這將在完成當前項目后跳轉到下一個項目。 另請注意,我沒有處理errorHandler
像這樣向getDetails()
添加回調:
function getDetails(productlist, callback)
{
inappbilling.getProductDetails(function successHandler2(result) {
callback(result[0].price);
}, errorHandler, productlist);
}
現在您可以使用調用getDetails()
的價格:
getDetails(res.product_id, function callback(price) {
// now you can update the DOM to show the actual price.
});
但是,我只會在檢索到價格后在列表中顯示游戲(將其添加到回調中的 DOM)。 否則它會在短時間內顯示為免費。
首先,確保所有 HTML 都被渲染,使用產品 ID 作為 class/id 或 HTML 屬性來區分不同的產品。
<li class='game-box showlist' data-productid='productId' >
...
</li>
然后,調用getProductDetails
,傳入指向產品的 HTML 選擇器。 您可以使用它在成功回調返回后更新 HTML,如下所示:
function getDetails(productlist, selector)
{
inappbilling.getProductDetails(function(result) {
var price= result[0].price;
$(selector).html(price);
}, errorHandler, productlist);
}
請注意,您可能希望使用另一個占位符,而不是將默認價格設置為“免費”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.