在 backbone js 中检查所有 rest 请求是否得到响应
Check whether all rest requests got response or not in backbone js
我想要一个回调函数,在 backbone js 中所有剩余请求都得到服务器的响应后将被调用。
只有所有请求得到响应后才会执行函数,而不是每个响应都执行。
我知道成功、错误和完成方法,但是,这些方法针对每个响应执行。
请告诉我是否存在任何其他函数或在从服务器收到所有响应后被调用。
我在下面发布了从服务器获取数据的代码。
fetchNextPage : function( successCallback, errorCallback, completeCallback, context )
{
this.requestNextPage({
remove : false,
success : function( collection, response, options ) {
if ( typeof successCallback === "function" )
{
successCallback.apply( context, [ collection, response, options ]);
}
},
error : function( collection, response, options ) {
if ( typeof errorCallback === "function" )
{
errorCallback.apply( context, [ collection, response, options ]);
}
},
complete: function( xhr, status ){
if ( typeof completeCallback === "function" )
{
completeCallback.apply( context, [ xhr, status ]);
}
}
});
}
您需要 jQuery $.when()
函数。
jQuery.when( deferreds )
Provides a way to execute callback functions based on one or more
objects, usually Deferred objects that represent asynchronous events.
每个 jQuery AJAX 请求将 return 一个 jqXHR object which is a part of jQuery's implementation of promises. The jqXHR object is returned synchronously, and then triggers again when the asynchronous operation it represents completes. You can then set a listener/callback on one (or more) of those promises with $.when()。异步操作的值return将被传递到then
包装的回调中。
$.when(jqXHR1, jqXHR2, jqXHR3).then(function(return1, return2, return3) {
//callback here will run only after the requests that returned the
//asyncronous operations linked to jqXHR 1-3 are completed.
});
我想要一个回调函数,在 backbone js 中所有剩余请求都得到服务器的响应后将被调用。 只有所有请求得到响应后才会执行函数,而不是每个响应都执行。
我知道成功、错误和完成方法,但是,这些方法针对每个响应执行。
请告诉我是否存在任何其他函数或在从服务器收到所有响应后被调用。
我在下面发布了从服务器获取数据的代码。
fetchNextPage : function( successCallback, errorCallback, completeCallback, context )
{
this.requestNextPage({
remove : false,
success : function( collection, response, options ) {
if ( typeof successCallback === "function" )
{
successCallback.apply( context, [ collection, response, options ]);
}
},
error : function( collection, response, options ) {
if ( typeof errorCallback === "function" )
{
errorCallback.apply( context, [ collection, response, options ]);
}
},
complete: function( xhr, status ){
if ( typeof completeCallback === "function" )
{
completeCallback.apply( context, [ xhr, status ]);
}
}
});
}
您需要 jQuery $.when()
函数。
jQuery.when( deferreds )
Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.
每个 jQuery AJAX 请求将 return 一个 jqXHR object which is a part of jQuery's implementation of promises. The jqXHR object is returned synchronously, and then triggers again when the asynchronous operation it represents completes. You can then set a listener/callback on one (or more) of those promises with $.when()。异步操作的值return将被传递到then
包装的回调中。
$.when(jqXHR1, jqXHR2, jqXHR3).then(function(return1, return2, return3) {
//callback here will run only after the requests that returned the
//asyncronous operations linked to jqXHR 1-3 are completed.
});