He creado una función de fábrica de emisor de eventos de fábrica multipropósito. Con él puedo convertir objetos en emisores de eventos. El código para la fábrica del emisor del evento está debajo si alguien desea echarle un vistazo o usarlo.Obtener una matriz de todos los eventos de DOM posibles
Mi pregunta es cómo puedo obtener una lista de eventos del DOM. Tenga en cuenta que no estoy tratando de obtener una lista de eventos combinados. Quiero una lista de todos los eventos posibles. Quiero agregar un método de "tubería" a los emisores. Este método tomaría un objeto DOM y se uniría a todos los posibles eventos, luego, cuando cualquiera de esos eventos disparara, se desencadenaría un evento con el mismo nombre en el emisor.
No creo que haya una forma de hacerlo. Estoy preparado para hacer una matriz codificada de nombres de eventos, pero si pudiera obtener la matriz para el DOM en su lugar sería mucho mejor y aún funcionaría si el W3C estandariza más tipos de eventos.
P.S. Si trabajas para el W3C, este es el tipo de basura que hace que todos odien el DOM. Por favor deja de tratar JavaScript como un lenguaje de juguete. No es un lenguaje de juguete y necesita más que tu juguete DOM.
/**
* Creates a event emitter
*/
function EventEmitter() {
var api, callbacks;
//vars
api = {
"on": on,
"trigger": trigger
};
callbacks = {};
//return the api
return api;
/**
* Binds functions to events
* @param event
* @param callback
*/
function on(event, callback) {
var api;
if(typeof event !== 'string') { throw new Error('Cannot bind to event emitter. The passed event is not a string.'); }
if(typeof callback !== 'function') { throw new Error('Cannot bind to event emitter. The passed callback is not a function.'); }
//return the api
api = {
"clear": clear
};
//create the event namespace if it doesn't exist
if(!callbacks[event]) { callbacks[event] = []; }
//save the callback
callbacks[event].push(callback);
//return the api
return api;
function clear() {
var i;
if(callbacks[event]) {
i = callbacks[event].indexOf(callback);
callbacks[event].splice(i, 1);
if(callbacks[event].length < 1) {
delete callbacks[event];
}
return true;
}
return false;
}
}
/**
* Triggers a given event and optionally passes its handlers all additional parameters
* @param event
*/
function trigger(event ) {
var args;
if(typeof event !== 'string' && !Array.isArray(event)) { throw new Error('Cannot bind to event emitter. The passed event is not a string or an array.'); }
//get the arguments
args = Array.prototype.slice.apply(arguments).splice(1);
//handle event arrays
if(Array.isArray(event)) {
//for each event in the event array self invoke passing the arguments array
event.forEach(function(event) {
//add the event name to the begining of the arguments array
args.unshift(event);
//trigger the event
trigger.apply(this, args);
//shift off the event name
args.shift();
});
return;
}
//if the event has callbacks then execute them
if(callbacks[event]) {
//fire the callbacks
callbacks[event].forEach(function(callback) { callback.apply(this, args); });
}
}
}
He hecho una pregunta similar: [¿Es posible atrapar programáticamente todos los eventos en la página en el navegador?] (Http://stackoverflow.com/questions/5107232/is-it-possible-to-programmatically -catch-all-events-en-la-página-en-el-navegador). –