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.