Created
September 25, 2013 10:13
-
-
Save mcfog/6697658 to your computer and use it in GitHub Desktop.
This file contains 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
<- define <[jquery]> | |
let @ = Deferred = {} | |
@when = $~when | |
@defer = -> | |
dfr = $.Deferred! | |
{ | |
resolver: dfr~{resolve, reject, notify, state} | |
promise: @_mkPromise dfr | |
} | |
#simple resolved promise creation | |
@resolve = (...args)~> | |
{resolver, promise} = @defer! | |
resolver.resolve ...args | |
promise | |
#simple rejected promise creation | |
@reject = (...args)~> | |
{resolver, promise} = @defer! | |
resolver.reject ...args | |
promise | |
#jQuery detection | |
@_mkPromise = \ | |
if ver = $::jquery | |
switch | |
#jQ1.5~1.7 use `pipe` instead of `then` | |
case ver.match /^1\.[5-7]/ | |
-> | |
promise = it.promise! | |
promise.then = -> | |
newPromise = @pipe ... | |
newPromise.then = @@callee | |
newPromise | |
promise | |
default | |
-> it.promise! | |
else | |
-> it.promise! | |
@ |
This file contains 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
(function(){ | |
var slice$ = [].slice; | |
define(['jquery'], function(){ | |
var Deferred; | |
return (function(){ | |
var ver, this$ = this; | |
this.when = bind$($, 'when'); | |
this.defer = function(){ | |
var dfr; | |
dfr = $.Deferred(); | |
return { | |
resolver: { | |
resolve: bind$(dfr, 'resolve'), | |
reject: bind$(dfr, 'reject'), | |
notify: bind$(dfr, 'notify'), | |
state: bind$(dfr, 'state') | |
}, | |
promise: this._mkPromise(dfr) | |
}; | |
}; | |
this.resolve = function(){ | |
var args, ref$, resolver, promise; | |
args = slice$.call(arguments); | |
ref$ = this$.defer(), resolver = ref$.resolver, promise = ref$.promise; | |
resolver.resolve.apply(resolver, args); | |
return promise; | |
}; | |
this.reject = function(){ | |
var args, ref$, resolver, promise; | |
args = slice$.call(arguments); | |
ref$ = this$.defer(), resolver = ref$.resolver, promise = ref$.promise; | |
resolver.reject.apply(resolver, args); | |
return promise; | |
}; | |
this._mkPromise = (ver = $.prototype.jquery) | |
? (function(){ | |
switch (false) { | |
case !ver.match(/^1\.[5-7]/): | |
return function(it){ | |
var promise; | |
promise = it.promise(); | |
promise.then = function(){ | |
var newPromise; | |
newPromise = this.pipe.apply(this, arguments); | |
newPromise.then = arguments.callee; | |
return newPromise; | |
}; | |
return promise; | |
}; | |
default: | |
return function(it){ | |
return it.promise(); | |
}; | |
} | |
}()) | |
: function(it){ | |
return it.promise(); | |
}; | |
return this; | |
}.call(Deferred = {})); | |
}); | |
function bind$(obj, key){ | |
return function(){ return obj[key].apply(obj, arguments) }; | |
} | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment