Created
May 12, 2013 13:27
-
-
Save clarkdave/5563555 to your computer and use it in GitHub Desktop.
Toggle a popup menu and close it when clicking outside (example using Backbone)
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
toggleUserMenu: (e) -> | |
e.preventDefault() if e | |
container = @$('.user') | |
# remove body listener if present | |
$('body').off 'click.menu' | |
$('.menu', container).toggleClass('visible') | |
container.toggleClass('open') | |
if container.hasClass('open') | |
# add listener to body to close menu if clicked outside it, but wait a moment or else it | |
# will trigger immediately and hide the menu | |
setTimeout ( => | |
$('body').on 'click.menu', (e) => | |
unless $(e.currentTarget).closest(container).length > 0 | |
@toggleUserMenu() | |
), 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment