什么是最干净的方式将JQuery Promise数组转换为数组的JQuery Promise? [英] What is cleanest way to turn Array of JQuery Promises into a JQuery Promise of an Array?
问题描述
我遇到了一个数组中有多个JQuery Promises的情况。
var arrayOfPromises = [$ .Deferred ,$ .Deferred(),$ .Deferred(),$ .Deferred()]
将它变成数组的JQuery Promise
var promiseOfArray = someTransform(arrayOfPromises)
其中
promiseOfArray.done (anArray){
alert(anArray.join(,));
});
创建一个包含文本的提醒
result1,result2,result3,result4
我目前定义 someTransform
in coffeescript as
someTransform =(arrayOfPromises) - >
$ .when(arrayOfPromises ...)。pipe(promises ...) - >
promises
转换为以下javascript
var someTransform,
__slice = [] .slice;
someTransform = function(arrayOfPromises){
return $ .when.apply($,arrayOfPromises).pipe(function(){
var promises;
promises = 1< = arguments.length?__slice.call(arguments,0):[];
return promises;
});
};
我想知道是否有一个更好的(更短,更清洁)的方式来定义 someTransform
以获得相同的结果?
://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply>应用数组作为 $。的参数当
。
var promiseOfArray = $ .when.apply($,arrayOfPromises);
为了使用更清楚,我喜欢添加一个方法到 $
:
$。whenall = function(arr){return $ .when.apply arr); };
现在您可以:
$。whenall([deferred1,deferred2,...])。
更新:默认情况下, done
处理程序将每个结果作为单独的参数传递;你不会得到一个结果数组。
由于你需要处理任意数量的Deferred,你可以使用特殊的隐式 arguments
对象以遍历结果。
$。whenall([d1,d2,...])done(function(){
for = 0; i //使用arguments [i]执行某些操作
}
});
如果你真的只想加入所有Deferred的字符串结果, hackery。 参数
是数组类,但不是数组
:
$。whenall([d1,d2,...])done(function(){
alert(Array.prototype.join.call ,'));
});
如果要返回结果数组您的完成
回调,我们可以调整 whenall
来执行它:
$。whenall = function(arr){
return $ .when.apply($,arr).pipe(function(){
return Array.prototype.slice.call(arguments);
});
};
I run into the situation where I have multiple JQuery Promises in an Array
var arrayOfPromises = [ $.Deferred(), $.Deferred(), $.Deferred(), $.Deferred() ]
and need to turn it into a JQuery Promise of an Array
var promiseOfArray = someTransform(arrayOfPromises)
where
promiseOfArray.done(function(anArray){
alert(anArray.join(","));
});
creates an alert with text
result1,result2,result3,result4
I currently define someTransform
in coffeescript as
someTransform = (arrayOfPromises) ->
$.when(arrayOfPromises...).pipe (promises...) ->
promises
which transforms to the following javascript
var someTransform,
__slice = [].slice;
someTransform = function(arrayOfPromises) {
return $.when.apply($, arrayOfPromises).pipe(function() {
var promises;
promises = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return promises;
});
};
Here is a jsFiddle of the result I'm looking for.
I was wondering if there is a better(shorter,cleaner) way to define someTransform
to achieve the same result?
You can just apply the array as the arguments to $.when
.
var promiseOfArray = $.when.apply($, arrayOfPromises);
To make the usage of this clearer, I like adding a method to $
:
$.whenall = function(arr) { return $.when.apply($, arr); };
Now you can do:
$.whenall([deferred1, deferred2, ...]).done(...);
Update: By default, the done
handler gets each result passed as a separate argument; you don't get an array of results.
Since you need to handle an arbitrary number of Deferreds, you can use the special implicit arguments
object to loop over the results.
$.whenall([d1, d2, ...]).done(function() {
for (var i = 0; i < arguments.length; i++) {
// do something with arguments[i]
}
});
If you really just want to join the string result of all your Deferreds, we can employ a little array hackery. arguments
is array-like, but is not an Array
:
$.whenall([d1, d2, ...]).done(function() {
alert(Array.prototype.join.call(arguments, ','));
});
If you want to return an array of results to your done
callback, we can tweak whenall
to do it:
$.whenall = function(arr) {
return $.when.apply($, arr).pipe(function() {
return Array.prototype.slice.call(arguments);
});
};
这篇关于什么是最干净的方式将JQuery Promise数组转换为数组的JQuery Promise?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!