Created
June 11, 2015 18:08
-
-
Save MiguelMadero/5dee03f5115770365c29 to your computer and use it in GitHub Desktop.
BodyEventListner
This file contains 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 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