Last active
December 17, 2015 17:49
-
-
Save ahomu/5648469 to your computer and use it in GitHub Desktop.
ZeptoのajaxをなんとなくFutureのpolyfill仕立てにする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @see https://github.com/slightlyoff/Futures | |
*/ | |
(function(Future, DollarLib) { | |
'use strict'; | |
function _wrap(func, wrapper) { | |
return function() { | |
return wrapper.apply(this, [func].concat(Array.prototype.slice.call(arguments, 0))); | |
}; | |
} | |
function _wrapCallback(wrapOf, resolverFunc) { | |
return _wrap(wrapOf, function() { | |
var origCallback = arguments[0], args = Array.prototype.slice.call(arguments, 1); | |
if (origCallback) { | |
origCallback.apply(null, args); | |
} | |
return resolverFunc.apply(null, args); | |
}); | |
} | |
DollarLib.ajax = _wrap(DollarLib.ajax, function(origAjax, options) { | |
return new Future(function(resolver) { | |
options || (options = {}); | |
options.success = _wrapCallback(options.success, resolver.resolve); | |
options.error = _wrapCallback(options.error, resolver.reject); | |
origAjax(options); | |
}); | |
}); | |
})(Future, $); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment