update Ox.Event (intermediate state)

This commit is contained in:
rolux 2012-06-29 14:24:06 +02:00
parent 1c23c80d3e
commit 28b9614083

View file

@ -8,9 +8,7 @@ Ox.Event <o> Basic event handler
Ox.Event = (function() {
var self = {}, that = {};
self.$eventHandler = $('<div>');
var that = {};
/*@
bind <f> Binds a callback to an event
@ -21,17 +19,84 @@ Ox.Event = (function() {
event <s> Event name
Event names can be namespaced, like `'click.foo'`
@*/
that.bind = function() {
Ox.forEach(Ox.makeObject(arguments), function(callback, event) {
var foo = event;
self.$eventHandler.on('ox_' + event, function(event, data) {
Ox.Log('Core', 'CALLBACK', foo, data.value);
callback(data.value);
that.bind = function(self) {
var args = Ox.slice(arguments, 1), once;
if (Ox.isBoolean(Ox.last(args))) {
once = args.pop();
}
if (Ox.isFunction(args[0])) {
args = {'': args[0]};
}
Ox.forEach(Ox.makeObject(args), function(callback, event) {
if (once) {
callback.once = true;
}
self.eventHandlers = self.eventHandlers || {};
self.eventHandlers[event] = (
self.eventHandlers[event] || []
).concat(callback);
});
return that;
};
that.bindOnce = function() {
return that.bind.apply(null, Ox.slice(arguments).concat(true));
};
/*
*/
that.trigger = function(self) {
if (!self.eventHandlers) {
return;
}
Ox.forEach(Ox.makeObject(
Ox.slice(arguments, 1, -1)
), function(data, event) {
triggered = event.split('.');
triggered.map(function(v, i) {
return triggered.slice(0, i + 1).join('.');
}).forEach(function(triggered) {
Ox.forEach(
Ox.extend(
{'': self.eventHandlers[''] || []},
triggered, self.eventHandlers[triggered] || []
),
function(handlers, handled) {
handler.once && that.unbind(self, triggered, handler);
handler(data || {}, event, Ox.last(arguments).oxid);
}
);
});
});
return that;
};
that.unbind = function(self) {
if (!self.eventHandlers) {
return;
}
var args = Ox.slice(arguments, 1);
if (args.length == 0) {
delete self.eventHandlers;
} else {
if (Ox.isFunction(args[0])) {
args = {'': args[0]};
}
Ox.forEach(Ox.makeObject(args), function(unbound, event) {
if (!unbound) {
delete self.eventHandlers[event];
} else {
self.eventHandlers[event].forEach(function(bound, i) {
if (bound == unbound) {
self.eventHandlers[event].splice(i, 1);
}
});
}
});
}
return that;
};
/*@
bindOnce <f> Binds a callback to an event, once
(event, callback) -> <o> The event handler