-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.wire.js
39 lines (36 loc) · 1.28 KB
/
jquery.wire.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
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define([], factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory();
} else {
root["mu-jquery-wire/jquery.wire"] = factory();
}
})(this, function () {
var slice = Array.prototype.slice;
function collect() {
return slice.call(arguments);
}
return function (input, callback) {
var me = this;
var $ = me.constructor;
var args = slice.call(arguments, 2);
var resolved = $.Deferred(function (dfd) {
dfd.resolveWith(me, [[]]);
});
return me.length === 0
? resolved
: $.when.apply(null, $.map(me, function (element, i) {
var $element = $(element);
return $.when($.isFunction(input) ? input.apply(me, [$element, i].concat(args)) : input).then(function (_input) {
return _input === undefined || _input.length === 0
? resolved
: $.when.apply(null, $.makeArray(_input).map(function (output, index) {
return $.when(callback.call(me, $element, index, output)).then(function (result) {
return arguments.length > 1 ? slice.call(arguments) : result || output;
});
})).then(collect);
});
})).then(collect);
}
});