Skip to content

Instantly share code, notes, and snippets.

@minhnc
Created April 8, 2012 00:21
Show Gist options
  • Save minhnc/2333095 to your computer and use it in GitHub Desktop.
Save minhnc/2333095 to your computer and use it in GitHub Desktop.
Remove All EventListeners
var win = Ti.UI.createWindow();
win.open();
/// <<< Register & UnRegister Event Listeners
/**
* params: {event: 'event', callback: eventCallback}
*/
function registerEventListener(obj, params) {
if ( typeof obj._eventListeners == 'undefined' ) {
obj._eventListeners = [];
}
obj.addEventListener(params.event, params.callback);
var eventListeners = obj._eventListeners;
eventListeners.push(params);
obj._eventListeners = eventListeners;
Ti.API.info( JSON.stringify(obj._eventListeners) );
}
function unRegisterAllEventListeners(obj) {
if ( typeof obj._eventListeners == 'undefined' || obj._eventListeners.length == 0 ) {
return;
}
for(var i = 0, len = obj._eventListeners.length; i < len; i++) {
var e = obj._eventListeners[i];
obj.removeEventListener(e.event, e.callback);
}
obj._eventListeners = [];
}
/// Register & UnRegister Event Listeners >>>
// Register some Events then Fire them
function event1(){ alert('event1'); }
function event2(){ alert('event2'); }
registerEventListener(win, {event: 'event1', callback: event1});
registerEventListener(win, {event: 'event2', callback: event2});
win.fireEvent('event1');
win.fireEvent('event2');
// call unRegisterAllEventListeners() to remove all event listeners
setTimeout(function(){
unRegisterAllEventListeners(win);
win.fireEvent('event1');
win.fireEvent('event2');
}, 1000);
@k-tten
Copy link

k-tten commented May 27, 2021

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment