Skip to content

Instantly share code, notes, and snippets.

@twalker
Last active December 20, 2015 05:09
Show Gist options
  • Save twalker/6076181 to your computer and use it in GitHub Desktop.
Save twalker/6076181 to your computer and use it in GitHub Desktop.
ready is Backbone view mixin to indicate when a view has rendered, returning a promise.
/**
* View.ready indicates when a view has rendered, returning a promise.
*
* Needs to be patched into a view's prototype due to the initialize and render methods.
* e.g. mixer.patch(View2.prototype, ready);
**/
define(function(require){
var jQuery = require('jquery');
return {
initialize: function(){
this._dfrReady = new jQuery.Deferred();
},
render: function(){
this._dfrReady.resolve(this);
return this;
},
ready: function(cb){
if(cb) this._dfrReady.done(cb);
return this._dfrReady.promise();
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment