First try at a jQuery plugin for event adding
Hi all,
Just after some guidance or suggestions as to weather I am on the right track, style etc.
I wanted to be able to insert an event at the top of an event stack in the new jQuery-1.4.2 special event handler.
I have written the following which accomplishes this for me, but want to know if I'm "doing it right".
- (function(jQuery){
- function modalEventAdd( obj ) {
- obj.origType = obj.type;
- if (obj.type.indexOf('modal') != -1) {
- obj.type = obj.type.split('modal')[1]
- } else {
- throw obj.type + ' is not modal supported';
- }
- var events = jQuery.data(this, "events");
- if (events[ obj.type ]) events[ obj.type ].reverse();
- jQuery.event.add( this, obj.type, obj, obj.data );
- if (events[ obj.type ]) events[ obj.type ].reverse();
- }
- jQuery.extend(jQuery.event.special, {
- modalkeydown : { add: modalEventAdd },
- modalkeypress : { add: modalEventAdd },
- modalkeyup : { add: modalEventAdd },
- })
- })(jQuery)
Now to add an event at the top of the event stack I can use something like the following:
- $(document).bind('modalkeydown', function(event){event.stopImmediatePropagation()});
Suggestions?
Should I do more error handling around line 10 for fetching data?
Should I cache events[ obj.type ] for reversing again after adding?
Should I not do this at all because of event handler position tracking?