Hi, I think this would be really useful! I've also modified jQuery to
do this a while ago (1.2.6) but with the new scope being the last
argument, so it works without the data object as well:

jQuery.fn.bind=function( type, data, fn, bind ) {
                return type == "unload" ? this.one(type, data, fn) : this.each
(function(){
                        if( $.isFunction(data) )
                                jQuery.event.add( this, type, data, bind, fn );
                        else
                                jQuery.event.add( this, type, fn, data, bind );
                });
        }


jQuery.event = {
        add: function(elem, types, handler, data, bind) {
                if ( elem.nodeType == 3 || elem.nodeType == 8 )
                        return;

                if( bind != undefined )
                        handler = jQuery.bind(handler, bind); //change scope
...


jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
        "mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave,"
+
        "change,select,submit,keydown,keypress,keyup,error").split(","),
function(i, name){

        // Handle event binding
        jQuery.fn[name] = function(fn, bind){ //second argument for the scope
                return fn ? this.bind(name, fn, bind) : this.trigger(name);
        };
});

where jQuery.bind is:

jQuery.bind=function( fn, bind ){
        var args = $.makeArray( arguments ).slice(2);
        if( args.length == 1 && $.isArray( args[0] ) )
                args = args[0];
        return function(){
                return fn.apply( bind, args );
        }
}



On Dec 25, 10:38 am, "Eduardo Lundgren" <eduardolundg...@gmail.com>
wrote:
> Hi guys,
>
> The .bind() method consider the scope of the handler the element whose the
> event is assigned - that is the correct as default.
>
> I've been playing with the event.js and implemented an alternative to call
> .bind(), specifying another scope, that looks useful for our api.
>
> I've attached the event.js modified from the rev. 5996 from the trunk.
> The changes are compatible with the current API.
>
> Here goes one example:
>
> var scopeTest = function() {
>                 this.name = "iamanotherscope";
>
>                 this.internalHandler = function(event) {
>                     console.log("I am another scope method.", this.name,
> event, event.data);
>                 };
>             };
>
>             var scope = new scopeTest();
>
>  $('div').bind('click', {data: true}, globalHandler);
>             $('div').bind('click', {data: true}, scope.internalHandler); //
> handler, data, default scope
>             $('div').bind('click', {data: true}, scope,
> scope.internalHandler); // handler, data, pre-defined scope
>
> Let me know if make sense for you.
>
> --
> Eduardo Lundgren
> Software Engineer
> Liferay, Inc.
> Enterprise. Open Source. For Life.
>
>  event.js
> 26KViewDownload

Reply via email to