First, this conversation started on the learningjquery.com blog
(http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind).

Ideally, I'd like to see the bind and unbind methods accept an object
of eventType/handler pairs, such that a single wrapped set could take
a hash of bindings in a single call. Something along these lines:

(function($) {

// Keep a copy of the old methods
$.fn._bind = $.fn.bind;
$.fn._unbind = $.fn.unbind;

// Redefine $().bind()
$.fn.bind = function( type, data, fn ) {
        // If only a map of handlers was passed...
        return (arguments.length === 1) ?

        this.each(function(key, node) {
                // Iterate over the map...
                $.each(type, function(event, handler) {
                        event == "unload" ?
                                // ... using $.fn.one() for "unload" events...
                                $(this).one(event, handler) :
                                // ... and $.event.add() for others
                                jQuery.event.add( this, event, handler );
                });
        }) :

        // Otherwise, use the existing implementation as of 1.3.2,
        // with slight syntactic modifications
        this.each(function(key, node) {
                type == "unload" ?
                        // Use $.fn.one() for "unload" events...
                        $(this).one(type, fn) :
                        // ... and $.event.add() for others
                        jQuery.event.add( this, type, fn || data, fn && data );
        });
};

// Redefine $().unbind()
$.fn.unbind = function(type, fn) {
        // If only a map of handlers was passed...
        return (arguments.length === 1)

        this.each(function(){
                // Iterate over the map...
                $.each(type, function(event, handler) {
                        // ... and unbind each using event.remove()
                        jQuery.event.remove( this, event, handler );
                });
        }) :

        // Otherwise, use the existing implementation as of 1.3.2,
        // copied verbatim
        this.each(function(){
                jQuery.event.remove( this, type, fn );
        });
};

})(jQuery);


Usage would be as follows:
$("#myID").bind({
        click : function(e) {
                // handle click
        },
        mouseover : function(e) {
                // handle mouseover
        },
        mouseout : function(e) {
                // handle mouseout
        }
});

Or, with named functions (ideal for later use of $().unbind()):
$("#myID").bind({
        click : clickHandler,
        mouseover : mouseoverHandler,
        mouseout : mouseoutHandler
});

function clickHandler(e) {
        // handle click
}

function mouseoverHandler(e) {
        // handle mouseover
}

function mouseoutHandler(e) {
        // handle mouseout
}

With named function references, unbinding multiple events would be
simple:
$("#myID").unbind({
        click : clickHandler,
        mouseover : mouseoverHandler,
        mouseout : mouseoutHandler
});


I realize there are some potential issues, such as the ability to pass
custom data to $().bind() is lost when an object of event/handler
pairs is passed. I personally don't usually use it anyhow, and binding
events individually will still support custom data (as the
implementation is the same).

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to