Skip to content

Instantly share code, notes, and snippets.

@asolove
Created September 4, 2012 19:22
Show Gist options
  • Save asolove/3625320 to your computer and use it in GitHub Desktop.
Save asolove/3625320 to your computer and use it in GitHub Desktop.
Subclass deferred
// 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