Created
September 4, 2012 19:22
-
-
Save asolove/3625320 to your computer and use it in GitHub Desktop.
Subclass deferred
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
// DeferredThingy is a class that present the interface of a deferred | |
// plus provides public methods that will only run after it is resolved. | |
function DeferredThingy(opts) { | |
this.opts = opts; | |
_.extend(this, new $.Deferred()); | |
} | |
DeferredThingy.prototype.ready = function(){ | |
this.resolve(data); | |
}; | |
DeferredThingy.prototype.renderEventually = afterResolved(function(data){ | |
this.render(data); | |
}; | |
// method decorator for calling this method only after deferred is resolved | |
// See: https://github.com/raganwald/homoiconic/blob/master/2012/08/method-decorators-and-combinators-in-coffeescript.md | |
function afterResolved(fn){ | |
return function(){ | |
var args = arguments, | |
self = this; | |
this.done(function(){ | |
fn.apply(self, args); | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment