Created
April 8, 2012 00:21
-
-
Save minhnc/2333095 to your computer and use it in GitHub Desktop.
Remove All EventListeners
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 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice