以前写的,后来想不起来了。写在这里以便查找
Deferred objects对我来说就是如果页面里面有两个ajax call对数据库进行操作。比如说两个grid 读取,Ajax执行异步操作的时候,页面显示loading...; 当两个操作都完成时,loading 隐藏
以下是代码片断
$(document).ready(function(e) {
var _loadingImageContainer = $('#IA5010_2_loadingImageContainer_outer');
_loadingImageContainer.removeClass("hidden");
var _outer_groupcategorysection =$('#outer_groupcategorysection');
var _outer_attr=$('#outer_attr');
_outer_groupcategorysection.addClass("hidden");
_outer_attr.addClass("hidden");
var get_para=JSON.stringify({ EnvironmentId: <%=Me.EnvironmentId %>, Company: <%=Me.CompanyNumber%>,
Department: '<%=IA5010_2_Department.Text%>', SizeratioId: <%=CInt(HiddenSizeRatioID.Value)%>});
var get_groupcategorysection=AjaxWrapper( '<%= ResolveUrl("~/IA_Allocation_Vb/IA5010D.aspx/GetGroupCategorySection")%>',get_para);
var get_attr=AjaxWrapper( '<%= ResolveUrl("~/IA_Allocation_Vb/IA5010D.aspx/GetProductAttributes")%>',
get_para);
var loadingdata=$.when (get_groupcategorysection,get_attr);
//success
loadingdata.done(function(data_groupcategorysection,data_attr){ //当两个异步操作成功的时候被调用
IA5010_1_populatedata(data_groupcategorysection,data_attr);
}); //loading data done
loadingdata.always(function(){ //无论成功与否,这个函数都会被调用。把loading隐藏写在这里
_loadingImageContainer.addClass("hidden");
_outer_groupcategorysection.removeClass("hidden");
_outer_attr.removeClass("hidden");
});
//deal with fail
loadingdata.fail(function(){
})
}); //document ready
function AjaxWrapper(webMethodrul, parameterListInJsonFormat) {
var promiseObject = $.ajax({
url: webMethodrul,
contentType: "application/json; charset=utf-8",
type: "POST",
data: parameterListInJsonFormat,
dataType: "json",
cache: false,
});
return promiseObject;
}