oxjs/source/Ox.UI/js/Core/Ox.Event.js

97 lines
2.8 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-09-23 10:43:57 +00:00
/*@
Ox.Event <o> Basic event handler
@*/
2011-09-23 10:43:57 +00:00
Ox.Event = (function() {
var self = {}, that = {};
self.$eventHandler = $('<div>');
/*@
bind <f> Binds a callback to an event
(event, callback) -> <o> The event handler
({event: callback, ...}) -> <o> The event handler
callback <f> Callback function
data <*> Event data
event <s> Event name
Event names can be namespaced, like <code>'click.foo'</code>
@*/
that.bind = function() {
Ox.forEach(Ox.makeObject(arguments), function(callback, event) {
2011-09-28 17:31:35 +00:00
var foo = event;
2012-05-28 14:06:22 +00:00
self.$eventHandler.on('ox_' + event, function(event, data) {
2011-11-04 15:54:28 +00:00
Ox.Log('Core', 'CALLBACK', foo, data.value);
2011-09-23 10:43:57 +00:00
callback(data.value);
});
});
return that;
};
/*@
bindOnce <f> Binds a callback to an event, once
(event, callback) -> <o> The event handler
({event: callback, ...}) -> <o> The event handler
callback <f> Callback function
data <*> Event data
event <s> Event name
Event names can be namespaced, like <code>'click.foo'</code>
@*/
that.bindOnce = function() {
Ox.forEach(Ox.makeObject(arguments), function(callback, event) {
self.$eventHandler.one('ox_' + event, function(event, data) {
callback(data.value);
});
});
return that;
};
/*@
trigger <function> Triggers an event
(event) -> <o> The event handler
(event, data) -> <o> The event handler
({event: data, ...}) -> <o> The event handler
event <s> Event name
data <*> Event data
@*/
that.trigger = function() {
Ox.forEach(Ox.makeObject(arguments), function(data, event) {
2011-11-04 15:54:28 +00:00
Ox.Log('Core', 'Ox.Event.trigger', event, data)
2011-09-23 10:43:57 +00:00
self.$eventHandler.trigger('ox_' + event, {value: data});
});
return that;
};
/*@
unbind <f> Unbinds all callbacks from an event
To unbind a specific handler, use namespaced events, like
<code>bind('click.foo', callback)</code>, and then
<code>unbind('click.foo')</code>.
() -> <o> The event handler
Unbinds all events
(event) -> <o> The event handler
Unbinds one event
(event, event, ...) -> <o> The event handler
Unbinds multiple events
([event, event, ...]) -> <o> The event handler
Unbinds multiple events
event <s> Event name
@*/
that.unbind = function() {
if (arguments.length == 0) {
2012-05-28 14:06:22 +00:00
self.$eventHandler.off();
2011-09-23 10:43:57 +00:00
} else {
Ox.toArray(arguments).forEach(function(event) {
2012-05-28 14:06:22 +00:00
self.$eventHandler.off('ox_' + event);
2011-09-23 10:43:57 +00:00
});
}
return that;
};
return that;
}());