Skip to content

Instantly share code, notes, and snippets.

@pcomans
Last active May 4, 2017 00:44
Show Gist options
  • Save pcomans/84c1985597f7509b9137244762ff02a9 to your computer and use it in GitHub Desktop.
Save pcomans/84c1985597f7509b9137244762ff02a9 to your computer and use it in GitHub Desktop.
Code to trigger a function when a backbone view is attached to the DOM. For those situations when you can't use Marionette's onShow.
var OnShowView = Backbone.View.extend({
onShow: function () {
console.log("Do something");
},
onRender: function () {
this.afterDomAttachment(this.onShow);
},
afterDomAttachment: function (afterAttachment) {
var that = this;
var attachmentCheck = function () {
if (that.$el.closest('html').length) {
// Element is attached to DOM
_.bind(afterAttachment, that)();
} else {
window.requestAnimationFrame(attachmentCheck);
}
};
window.requestAnimationFrame(attachmentCheck);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment