मैंने एक बहुउद्देशीय फैक्ट्री इवेंट उत्सर्जक फैक्ट्री फ़ंक्शन बनाया है। इसके साथ मैं वस्तुओं emitters में वस्तुओं को बदल सकते हैं। इवेंट उत्सर्जक फैक्ट्री के लिए कोड नीचे है यदि कोई भी देखना या इसका उपयोग करना चाहेगा।सभी डीओएम कार्यक्रमों की एक सरणी प्राप्त करना संभव है
मेरा सवाल है कि मैं डीओएम से घटनाओं की सूची कैसे प्राप्त कर सकता हूं। कृपया ध्यान दें कि मैं बाध्य घटनाओं की सूची प्राप्त करने की कोशिश नहीं कर रहा हूं। मैं सभी घटनाओं की एक सूची संभव चाहता हूं। मैं उत्सर्जकों के लिए एक "पाइप" विधि जोड़ना चाहता हूं। यह विधि एक डीओएम ऑब्जेक्ट लेगी और सभी संभावित घटनाओं से जुड़ जाएगी, फिर जब उनमें से कोई भी घटना आग लगती है तो प्रत्येक एमिटर में एक ही नाम की घटना को ट्रिगर करेगा।
मुझे कल्पना नहीं है कि ऐसा करने का कोई तरीका है। मैं घटना नामों का एक कठिन कोडित सरणी बनाने के लिए तैयार हूं, लेकिन अगर मैं डीओएम के लिए सरणी प्राप्त कर सकता हूं तो यह बेहतर होगा और डब्ल्यू 3 सी अधिक घटना प्रकारों को मानकीकृत करता है, फिर भी काम करेगा।
पीएस यदि आप डब्ल्यू 3 सी के लिए काम करते हैं तो यह बकवास है जो हर किसी को डोम से नफरत करता है। कृपया एक खिलौना भाषा की तरह जावास्क्रिप्ट का इलाज बंद करो। यह खिलौना भाषा नहीं है और आपके खिलौने डोम से अधिक की जरूरत है।
/**
* 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); });
}
}
}
मैंने एक समान प्रश्न पूछा है: [क्या ब्राउज़र में पृष्ठ पर सभी घटनाओं को प्रोग्रामेटिक रूप से पकड़ना संभव है?] (Http://stackoverflow.com/questions/5107232/is-it-possible-to- प्रोग्रामेटिक रूप से -catch-सभी घटनाओं-ऑन-द पेज-इन-द-ब्राउज़र)। –