Created
November 15, 2012 17:59
-
-
Save animecyc/4080118 to your computer and use it in GitHub Desktop.
Titanium Event Management
This file contains hidden or 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
/*global Ti*/ | |
var _ = require('/application/shared/lib/underscore'); // needs underscore | |
exports.attach = function (proxy, to_attach, named, prefix) { | |
'use strict'; | |
var reservedEvents = ['pause', 'paused', 'resume', 'resumed', 'orientationchange']; | |
if (proxy && _.size(to_attach)) { | |
prefix = (prefix ? prefix + ':' : ''); | |
proxy._events = (function () { | |
var _events = proxy._events || {}; | |
_.each(to_attach, function (callback, event) { | |
if (_.has(named, event)) { | |
_events[named[event]] = { | |
name : prefix + event, | |
method : callback | |
}; | |
} else { | |
_events[prefix + event] = callback; | |
} | |
}); | |
return _events; | |
}()); | |
_.each(to_attach, function (callback, events) { | |
_.each(events.split(','), function (event) { | |
var _event = (_.contains(reservedEvents, event) ? event : prefix + event); | |
proxy.addEventListener(_event, callback); | |
}); | |
}); | |
} | |
return proxy; | |
}; | |
exports.detach = function (proxy, to_detach) { | |
'use strict'; | |
if (proxy && _.has(proxy, '_events') && to_detach) { | |
_.each(proxy._events, function (callback, event) { | |
if (to_detach === event) { | |
if (_.isFunction(callback)) { | |
proxy.removeEventListener(event, callback); | |
} else { | |
proxy.removeEventListener(callback.name, callback.method); | |
} | |
proxy._events = _.omit(proxy._events, event); | |
} | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment