Skip to content

Instantly share code, notes, and snippets.

@MiguelMadero
Created June 11, 2015 18:08
Show Gist options
  • Save MiguelMadero/5dee03f5115770365c29 to your computer and use it in GitHub Desktop.
Save MiguelMadero/5dee03f5115770365c29 to your computer and use it in GitHub Desktop.
BodyEventListner
var BodyEventListener = Ember.Mixin.create({
bodyElementSelector: 'html',
bodyClick: Ember.K,
bubbleEvents: false,
didInsertElement: function() {
this._super();
return Ember.run.next(this, this._setupDocumentHandlers);
},
willDestroyElement: function() {
this._super();
return this._removeDocumentHandlers();
},
_setupDocumentHandlers: function() {
var _this = this;
if (this.get('isDestroyed') || this._clickHandler) {
return;
}
this._clickHandler = function(event) {
if (event.target.id === _this.elementId || !Em.isEmpty($(event.target).parents('#' + _this.elementId))) {
return _this.get('bubbleEvents');
} else {
return _this.bodyClick(event.target);
}
};
return $(this.get('bodyElementSelector')).on('click', this._clickHandler);
},
_removeDocumentHandlers: function() {
$(this.get('bodyElementSelector')).off('click', this._clickHandler);
this._clickHandler = null;
}
});
export default BodyEventListener;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment