+1
The ability to specify the scope to a handler function would be a
great addition to jQuery core.

On Apr 8, 12:03 pm, gregory <gregory.tomlin...@gmail.com> wrote:
> the only difficulty I am having with
> Balazs Endresz's approach (which I have also
> implemented in my environment) is if another developer passes a
> function as 'data' param, the results become unpredictable. Though I
> don't *think* anybody should be passing a function to access as
> event.data, it currently does work to do so.
>
> though changing the pattern to no longer have the handler as the last
> param may cause minor confusion, it should not cause any backward
> compatibility issues.
>
> I have never bench marked the performance of 'return toString.call
> (obj) === "[object Function]";' Is this faster than running typeof obj
> === "function" ?
>
> very, very interested in seeing the core of jquery improved to include
> a capability to apply correct scope to the handler function
>
> thanks!
> -gregory
>
> On Mar 29, 3:26 am, Azat Razetdinov <razetdi...@gmail.com> wrote:
>
> > From the updated jQuery 1.4 Roadmap:
>
> > > If you need a different object for the scope, why not use the data 
> > > argument to transport it?
>
> > In OOP-style applications the handler is often not an anonymous
> > function but a link to the current objects's prototype method:
>
> > this._input.bind('change', this._onInputChange, this);
>
> > And all prototype methods expect that 'this' points to the current
> > object. If one needs the jQuery object, he could happily use
> > event.currentTarget to reach it.
>
> > One would recommend binding all handlers with anonymous functions,
> > e.g.:
>
> > var that = this;
> > this._input.bind('change', function (event) { that._onInputChange
> > (event) });
>
> > 1. It's more verbose. 2. There's no way to unbind this handler.
>
> > On Feb 23, 11:56 pm, Azat Razetdinov <razetdi...@gmail.com> wrote:
>
> > > Passing handler after scope is not suitable for two reasons:
>
> > > 1. There's no way to determine whether data or scope is passed in a
> > > three-argument method call.
> > > 2. Passing scope after handler is common pattern in JavaScript 1.6
> > > methods like forEach.
>
> > > On Dec 25 2008, 11:08 pm, "Eduardo Lundgren"
>
> > > <eduardolundg...@gmail.com> wrote:
> > > > The isFunction is faster now but still has more coast that when you 
> > > > don't
> > > > need to call it.
>
> > > > We should keep the handler as the last parameter to fit with the jQuery 
> > > > API,
> > > > the change is compatible with it.
>
> > > >   $('div').bind('click', {data: true}, scope, *scope.internalHandler*);
>
> > > > Scoping events is a good addition to jQuery.
>
> > > > Ariel, Joern, John? Let me know if it make sense for you.
>
> > > > Thanks,
> > > > Eduardo Lundgren
>
> > > > On Thu, Dec 25, 2008 at 11:57 AM, Balazs Endresz
> > > > <balazs.endr...@gmail.com>wrote:
>
> > > > > True, but the new isFunction is a couple of times faster than the old
> > > > > one, though it's still many times faster to directly call
> > > > > Object.prototype.toString, which is far below 0.001ms. But as the
> > > > > callback function is the last parameter everywhere in jQuery it might
> > > > > be confusing to change this pattern, it just looked more like binding
> > > > > the function with a native method for me.
>
> > > > > On Dec 25, 7:06 pm, "Eduardo Lundgren" <eduardolundg...@gmail.com>
> > > > > wrote:
> > > > > > Hi Balazs,
>
> > > > > > Thanks for give us your opinion.
>
> > > > > > When you use $.isFunction(data) on the bind method it is very 
> > > > > > expensive
> > > > > when
> > > > > > you have a lot of iterations.
>
> > > > > > Diff the file I attached with the original file (rev. 5996) I made 
> > > > > > only a
> > > > > > small change on the bind() method, and it's compatible with data 
> > > > > > and with
> > > > > > out API.
>
> > > > > > On Thu, Dec 25, 2008 at 3:05 AM, Balazs Endresz <
> > > > > balazs.endr...@gmail.com>wrote:
>
> > > > > > > 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
>
> > > > > > --
> > > > > > Eduardo Lundgren
> > > > > > Software Engineer
> > > > > > Liferay, Inc.
> > > > > > Enterprise. Open Source. For Life.
>
> > > > --
> > > > Eduardo Lundgren
> > > > Software Engineer
> > > > Liferay, Inc.
> > > > Enterprise. Open Source. For Life.

Reply via email to