Skip to content

Instantly share code, notes, and snippets.

@gmac
Created February 13, 2013 22:55
Show Gist options
  • Save gmac/4949156 to your computer and use it in GitHub Desktop.
Save gmac/4949156 to your computer and use it in GitHub Desktop.
Click-off dismissal behavior for a component that should close upon any mouse click, touch, or resize.
require("nixable", [
"jquery",
"underscore"
], function( $, _ ) {
return {
nixable: function( closeTest, closeCall, context ) {
var self = this;
// Clear any outstanding nix call:
if ( this._nix ) {
this._nix();
}
// Store new nix call:
this._nix = _.bind( closeCall, context );
// Nixable events:
var events = "mousedown.nix touchstart.nix resize.nix";
// Configure nixable behavior:
var doc = $( document )
.off( events )
.on( events, function( evt ) {
if ( closeTest.call( context, evt ) ) {
doc.off( events );
self._nix();
self._nix = null;
}
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment