-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-when-always.js
77 lines (73 loc) · 2.79 KB
/
jquery-when-always.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* jQuery/Zepto plugin to allow always and fail for $.when-like statements
* to resolve similar to done. All defers wait for full-set completion until the
* master defer (@see $.when) is resolved/rejected.
* @param {function} $ jQuery or Zepto
*
* @author Russell Munson
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], function (jQuery) {
return factory(jQuery);
});
} else if (typeof module !== 'undefined' && module.exports) {
var jQuery = require('jquery');
module.exports = factory( jQuery );
} else{
factory(jQuery);
}
})(this, function( jQuery ) {
var defFilter=jQuery.Deferred().pipe ? 'pipe' : 'then',
sliceArgs=Array.prototype.slice;
//Zepto is little finiky with it's extend, so suppy a start target
jQuery.extend(jQuery,{
whenAlways : function(){
var args=sliceArgs.call(arguments,0),
count=args.length,
finalQueue=$.Callbacks("once"),
finalVals=[],
rejected=false,
resFunc=function(originalDef,index){
return function(val){
var retArgs=arguments.length > 1 ? sliceArgs.call(arguments,0) : val;
finalVals[index]=retArgs;
if(!(--count)){
finalQueue.fire()
}
return retArgs;
};
},
failFunc=function(originalDef,index){
return function(val){
var safeDef=$.Deferred(),
retArgs=arguments.length > 1 ? sliceArgs.call(arguments,0) : val ;
rejected=true;
finalVals[index]=retArgs;
if(!(--count)){
finalQueue.fire();
return retArgs;
}
finalQueue.add(function(){
safeDef.rejectWith(originalDef,retArgs);
});
return safeDef;
}
};
return $.when.apply(
$.when,$.map(args,function(od,i){
return od && od[defFilter] ? od[defFilter](
//Success
resFunc(od,i),
//Fail
failFunc(od,i)
) : (count--,finalVals[i]=od,od);
})
)[defFilter](null,function(){
return $.Deferred().rejectWith(this,finalVals);
});
}
});
return jQuery;
});