Event Handler

Event Handler

Event Handler plugin was made to make handling your event handlers a little easier. It adds one new method called eventH to jQuery which can only be invoked using $.eventH(). It expects two arguments (action, options) which must follow the following guidelines:
action must be a string == "bind" OR "unbind" OR "rebind" OR "add" OR "remove"
options must be an object (hash) that may contain the following:

  1. useList: { 
  2.     key: "the value must be set, but is not used", 
  3.     anotherKey: null, 
  4.     keying: "Bla", 
  5.     hex: null 
  6.   } 
  7.   skipList: { 
  8.     key: "same rules as useList" 
  9.   } 
  10.   newHandler: { 
  11.     type: ["click", "mouseover", "mouseout"], 
  12.     name: "key", 
  13.     selector: "jQuery Selector", 
  14.     func: [ 
  15.       function(){ 
  16.         //Click 
  17.       }, 
  18.       function(){ 
  19.         //MouseOver 
  20.       }, 
  21.       function(){ 
  22.         //MouseOut 
  23.       } 
  24.     ] 
  25.   }, 
  26.   handlers: [ 
  27.     {type:["change", "click", "focus", "blur"], name: "key", selector: "p", func: [function(){ 
  28.         //Change 
  29.       }, 
  30.       function(){ 
  31.         //Click 
  32.       }, 
  33.       function(){ 
  34.         //Focus 
  35.       }, 
  36.       function(){ 
  37.         //Blur 
  38.       } 
  39.     }, 
  40.     {type:["change", "click", "focus", "blur"], name: "key", selector: "p", func: [function(){ 
  41.         //Change 
  42.       }, 
  43.       function(){ 
  44.         //Click 
  45.       }, 
  46.       function(){ 
  47.         //Focus 
  48.       }, 
  49.       function(){ 
  50.         //Blur 
  51.       } 
  52.     },... 
  53.   ] 

NOTE: handlers should be passed the first time the function is invoked and should not be passed again (if it is, it will be discarded). Use the newHandler option and the add action to add new handlers.

The idea is that you call the eventH() method inside the jQuery ready function with action == "bind" and options containing just the handlers array. The keys that you define can be used in the useList and skipList when calling "bind" OR "unbind" OR "rebind" OR "remove" ("add" doesn't use the useList or skipList).

The "rebind" action is useful when you have an element who's content keeps changing (completely) but keeps the same selectors there.
For Example:

  1. <div class='cont'> 
  2.   <span class='name'>Name</span> 
  3. </div>  

Now lets say you changed the content of the div with $(".cont").html("Name 2");. Any event handlers you had registered with the span would be lost. If you call the rebind action with the key you defined for that span, you can recover all event handlers. (NOTE: This is only common in ajax applications where the server is returning HTML).

The "unbind" action is useful if you want to temporarily remove all event handlers from an element (or set of elements). It is also internally used for the "rebind" action.

The "bind" action can be called if/when you want to recover the event handlers you unbound with the "unbind" action.

The "add" action is useful if you want to add an element to the list for lator use with "unbind"/"rebind"/"bind". Or if you want to add multiple event handlers to a single group of evelent(s) (eg. mouseover, mouseout, click...) all at once. The normal jQuery way of doing this would be like this: $("selector").mouseover(...).mouseout(...).click(...);

This plugin is no where near perfect and I'm hoping to have some time to improve it.


转载请注明:代码家园 » Event Handler