Last active
May 4, 2017 00:44
-
-
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.
This file contains hidden or 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
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