Created
September 17, 2014 13:27
-
-
Save danmartens/b1c4a5441e3f06641e60 to your computer and use it in GitHub Desktop.
Ember ClickOutside Mixin
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 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