Skip to content

Instantly share code, notes, and snippets.

@danmartens
Created September 17, 2014 13:27
Show Gist options
  • Save danmartens/b1c4a5441e3f06641e60 to your computer and use it in GitHub Desktop.
Save danmartens/b1c4a5441e3f06641e60 to your computer and use it in GitHub Desktop.
Ember ClickOutside Mixin
var ClickOutside = Ember.Mixin.create({
listenForClickOutside: false,
clickOutside: Ember.K,
_delegateClickOutside: function() {
$(window).on('mousedown', $.proxy(this._handleClickOutside, this));
},
_undelegateClickOutside: function() {
$(window).off('mousedown', this._handleClickOutside);
}.on('willDestroyElement'),
_handleClickOutside: function(event) {
if (!this.$().find(event.target).length) {
this.clickOutside(event);
this.trigger('clickOutside');
}
},
_listenForClickOutsideDidChange: function() {
Ember.run.scheduleOnce('afterRender', this, function() {
if (this.get('listenForClickOutside')) {
this._delegateClickOutside();
} else {
this._undelegateClickOutside();
}
});
}.observes('listenForClickOutside').on('didInsertElement')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment