2012-02-20 17 views
6

मैंने एक बहुउद्देशीय फैक्ट्री इवेंट उत्सर्जक फैक्ट्री फ़ंक्शन बनाया है। इसके साथ मैं वस्तुओं 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); }); 
     } 
    } 
} 
+0

मैंने एक समान प्रश्न पूछा है: [क्या ब्राउज़र में पृष्ठ पर सभी घटनाओं को प्रोग्रामेटिक रूप से पकड़ना संभव है?] (Http://stackoverflow.com/questions/5107232/is-it-possible-to- प्रोग्रामेटिक रूप से -catch-सभी घटनाओं-ऑन-द पेज-इन-द-ब्राउज़र)। –

उत्तर

5

यहां एक संस्करण है जो क्रोम, सफारी और एफएफ में काम करता है।

Object.getOwnPropertyNames(document).concat(Object.getOwnPropertyNames(Object.getPrototypeOf(Object.getPrototypeOf(document)))).filter(function(i){return !i.indexOf('on')&&(document[i]==null||typeof document[i]=='function');}) 

युपीडी:

और यहाँ संस्करण IE9 +, क्रोम, सफारी और एफएफ में काम करती है।

Object.getOwnPropertyNames(document).concat(Object.getOwnPropertyNames(Object.getPrototypeOf(Object.getPrototypeOf(document)))).concat(Object.getOwnPropertyNames(Object.getPrototypeOf(window))).filter(function(i){return !i.indexOf('on')&&(document[i]==null||typeof document[i]=='function');}).filter(function(elem, pos, self){return self.indexOf(elem) == pos;}) 

पुनश्च: परिणाम घटनाओं की एक सरणी ["onwebkitpointerlockerror", "onwebkitpointerlockchange", "onwebkitfullscreenerror", "onwebkitfullscreenchange", "onselectionchange", "onselectstart", "onsearch", "onreset", "onpaste", "onbeforepaste", "oncopy"] ... ect की तरह नाम है।

5

सभी डोम घटनाएं on से शुरू होती हैं। आप Element इंस्टेंस के माध्यम से लूप कर सकते हैं, और on से शुरू होने वाली सभी गुणों को सूचीबद्ध कर सकते हैं।

उदाहरण। कंसोल में निम्न कोड (फ़ायरफ़ॉक्स, सरणी comprehensions उपयोग करते हुए;) को कॉपी-पेस्ट):

// event handler IDL attributes 
    [TreatNonCallableAsNull] attribute Function? onabort; 
    [TreatNonCallableAsNull] attribute Function? onblur; 
    [TreatNonCallableAsNull] attribute Function? oncanplay; 
    [TreatNonCallableAsNull] attribute Function? oncanplaythrough; 
    [TreatNonCallableAsNull] attribute Function? onchange; 
    [TreatNonCallableAsNull] attribute Function? onclick; 
    [TreatNonCallableAsNull] attribute Function? oncontextmenu; 
    [TreatNonCallableAsNull] attribute Function? oncuechange; 
    [TreatNonCallableAsNull] attribute Function? ondblclick; 
    [TreatNonCallableAsNull] attribute Function? ondrag; 
    [TreatNonCallableAsNull] attribute Function? ondragend; 
    [TreatNonCallableAsNull] attribute Function? ondragenter; 
    [TreatNonCallableAsNull] attribute Function? ondragleave; 
    [TreatNonCallableAsNull] attribute Function? ondragover; 
    [TreatNonCallableAsNull] attribute Function? ondragstart; 
    [TreatNonCallableAsNull] attribute Function? ondrop; 
    [TreatNonCallableAsNull] attribute Function? ondurationchange; 
    [TreatNonCallableAsNull] attribute Function? onemptied; 
    [TreatNonCallableAsNull] attribute Function? onended; 
    [TreatNonCallableAsNull] attribute Function? onerror; 
    [TreatNonCallableAsNull] attribute Function? onfocus; 
    [TreatNonCallableAsNull] attribute Function? oninput; 
    [TreatNonCallableAsNull] attribute Function? oninvalid; 
    [TreatNonCallableAsNull] attribute Function? onkeydown; 
    [TreatNonCallableAsNull] attribute Function? onkeypress; 
    [TreatNonCallableAsNull] attribute Function? onkeyup; 
    [TreatNonCallableAsNull] attribute Function? onload; 
    [TreatNonCallableAsNull] attribute Function? onloadeddata; 
    [TreatNonCallableAsNull] attribute Function? onloadedmetadata; 
    [TreatNonCallableAsNull] attribute Function? onloadstart; 
    [TreatNonCallableAsNull] attribute Function? onmousedown; 
    [TreatNonCallableAsNull] attribute Function? onmousemove; 
    [TreatNonCallableAsNull] attribute Function? onmouseout; 
    [TreatNonCallableAsNull] attribute Function? onmouseover; 
    [TreatNonCallableAsNull] attribute Function? onmouseup; 
    [TreatNonCallableAsNull] attribute Function? onmousewheel; 
    [TreatNonCallableAsNull] attribute Function? onpause; 
    [TreatNonCallableAsNull] attribute Function? onplay; 
    [TreatNonCallableAsNull] attribute Function? onplaying; 
    [TreatNonCallableAsNull] attribute Function? onprogress; 
    [TreatNonCallableAsNull] attribute Function? onratechange; 
    [TreatNonCallableAsNull] attribute Function? onreset; 
    [TreatNonCallableAsNull] attribute Function? onscroll; 
    [TreatNonCallableAsNull] attribute Function? onseeked; 
    [TreatNonCallableAsNull] attribute Function? onseeking; 
    [TreatNonCallableAsNull] attribute Function? onselect; 
    [TreatNonCallableAsNull] attribute Function? onshow; 
    [TreatNonCallableAsNull] attribute Function? onstalled; 
    [TreatNonCallableAsNull] attribute Function? onsubmit; 
    [TreatNonCallableAsNull] attribute Function? onsuspend; 
    [TreatNonCallableAsNull] attribute Function? ontimeupdate; 
    [TreatNonCallableAsNull] attribute Function? onvolumechange; 
    [TreatNonCallableAsNull] attribute Function? onwaiting; 

    // special event handler IDL attributes that only apply to Document objects 
    [TreatNonCallableAsNull,LenientThis] attribute Function? onreadystatechange; 
+0

नोट।घटनाओं की पिछली सूची ** पूर्ण नहीं है **। उदाहरण के लिए, ['' तत्व] (http://dev.w3.org/html5/spec/Overview.html#the-body-element) भी ईवेंट के एक सेट को परिभाषित करता है। बस '[TreatNonCallableAsNull] विशेषता फ़ंक्शन के लिए खोजें? सभी (एचटीएमएल 5) घटनाओं को खोजने के लिए spec में। –

+0

मैंने पहले से ही डीओएम स्तर 0 घटनाओं के माध्यम से लूपिंग करने का प्रयास किया है और वे लूप में नहीं दिखते हैं क्योंकि * विधियों को शून्य के रूप में छोड़े जाने पर गैर-गणना योग्य नहीं है। कृपया ध्यान दें कि मैं मौजूदा बाइंडिंग को कैप्चर करने की कोशिश नहीं कर रहा हूं। मैं संभावित बाइंडिंग की गतिशील सूची प्राप्त करने की कोशिश कर रहा हूं। –

+0

@RobertHurst अनुरूप (आधुनिक) ब्राउज़र में, सभी घटनाएं संख्यात्मक हैं। जब उन्हें अभी तक परिभाषित नहीं किया गया है, तो उन्हें प्रति परिभाषा 'शून्य' होना चाहिए। चूंकि घटनाएं ** पहले से ज्ञात हैं **, मैं घटनाओं की एक सूची बनाने और इसे कार्यान्वित करने की अनुशंसा करता हूं। यह कई तत्वों के गुणों के माध्यम से लूपिंग और संपत्ति नामों को फ़िल्टर करने से कहीं अधिक कुशल है। –

0

मैं: घटनाओं पाने के लिए

[i for(i in document)].filter(function(i){return i.substring(0,2)=='on'&&(document[i]==null||typeof document[i]=='function');}) 

एक और तरीका है जो पता चलता है, the specification को देखकर है मैंने कल्पना पढ़ी है और मैंने पुष्टि की है कि यह वर्तमान में संभव नहीं है। हमें सबसे बुनियादी पर्यावरण प्रदान नहीं करने के लिए धन्यवाद डब्ल्यू 3 सी!

हालांकि मैं कुछ भी बलि किए बिना इस मुद्दे को हल करने में कामयाब रहा। जैसा कि मैंने कहा था कि पाइप से पहले एक उत्सर्जक (जैसे कि डोम नोड) से किसी भी घटना को वर्तमान उत्सर्जक में ले जाता है। हालांकि मुझे किसी भी घटना को सुनने की कोशिश करने तक कुछ भी करने की आवश्यकता नहीं है। आंतरिक रूप से मैं जो करता हूं वह पाइप एमिटर से बांधता है क्योंकि लोग वर्तमान उत्सर्जक से बांधते हैं।

मेरे पास released the library है यदि मैंने यह देखने के लिए उत्सुक है कि मैंने क्या किया है। पाइप के लिए कोड pipe() विधि और on() विधि में है।

संबंधित मुद्दे