Created
February 7, 2012 16:35
-
-
Save almost/1760582 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
## chainDeferred | |
# Take two jQuery Deferred objects and chain one on to the other. So | |
# anything that happens to the first one is proxied on the second one. | |
# Returns the source (for method call chaining) | |
module.chainDeferred = (source, destination) -> | |
source.then( | |
-> destination.resolveWith(@, arguments), | |
-> destination.rejectWith(@, arguments), | |
-> destination.notifyWith(@, arguments) | |
) | |
source | |
## chainError | |
# Take two jQuery Deferred objects and forward any erros on the first | |
# on to the second. Return the source (for method chaining) | |
module.chainError = (source, destination) -> | |
source.fail(-> destination.rejectWith(@,arguments)) | |
source | |
fetchRelated: (name) => | |
deferred = jQuery.Deferred() | |
utils.chainError(@maybeFetch, deferred).done => | |
obj = @related(name) | |
if obj | |
utils.chainDeferred(obj.maybeFetch(), deferred) | |
else | |
deferred.rejectWith(@, ['Link does not exist']) | |
return deferred.promise() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment