Last active
August 23, 2017 16:24
-
-
Save dmytro-y-dev/2dc14f13ee1ba7f77f1479fb53ebd618 to your computer and use it in GitHub Desktop.
Example of how to accurately detach your event from jQuery handlers.
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
const table; /* DOM Element that reacts to resize event */ | |
// Initialize table | |
const __sync = () { /* some happy crappy code */ }; | |
$(window).on('resize', null, { | |
type: 'sticky-table-handler', | |
table: table | |
}, __sync); | |
__sync(); | |
// ... User plays around with table here ... | |
// Bye table! Let's remove resize event that was assigned for specific table. | |
$.each( | |
$._data(window, 'events').resize, | |
(index, eventHandler) => { | |
if (eventHandler.data && | |
eventHandler.data.type === 'sticky-table-handler' && | |
eventHandler.data.table === table) | |
{ | |
$._data(window, 'events').resize.splice(index, 1); | |
return false; | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment