Ho creato una funzione factory emettitore eventi multiuso. Con esso posso trasformare gli oggetti in emettitori di eventi. Il codice per la fabbrica di emettitori di eventi è sotto se qualcuno vorrebbe dare un'occhiata o usarlo.Come ottenere una matrice di tutti gli eventi DOM possibili
La mia domanda è come ottenere un elenco di eventi dal DOM. Nota. Non sto cercando di ottenere un elenco di eventi associati. Voglio una lista di tutti gli eventi possibili. Voglio aggiungere un metodo "pipe" agli emettitori. Questo metodo prenderebbe un oggetto DOM e si collegherebbe a tutti gli eventi possibili, quindi quando uno di quegli eventi sparasse ognuno avrebbe attivato un evento con lo stesso nome nell'emettitore.
Non immagino ci sia un modo per farlo. Sono pronto a creare un array di nomi di eventi, ma se riesco a ottenere l'array per il DOM, sarebbe molto meglio e funzionerebbe ancora se il W3C standardizzasse più tipi di eventi.
P.S. Se lavori per il W3C questo è il tipo di schifezza che fa odiare tutti il DOM. Si prega di smettere di trattare JavaScript come un linguaggio giocattolo. Non è un linguaggio giocattolo e ha bisogno di più del tuo DOM giocattolo.
/**
* 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); });
}
}
}
Ho fatto una domanda simile: [È possibile registrare tutti gli eventi sulla pagina nel browser?] (Http://stackoverflow.com/questions/5107232/is-it-possibile-per-programmaticamente -Catch-all-eventi-on-the-page-in-the-browser). –