I added in jQuery.proxy( obj, name ) support as well (I like this - I
also showed how to do it in Secrets of the JavaScript Ninja:
http://github.com/jquery/jquery/commit/1d2b1a57dae0b73b3d99197f73f4edb623b5574a

Any major concerns before I push this through? Will this meet the
needs of everyone in the thread?

--John



On Thu, Dec 31, 2009 at 12:39 AM, John Resig <jere...@gmail.com> wrote:
> So I definitely agree that having a single, one-off, API addition (to
> bind and live) is kind of lame - especially when it conflicts with the
> jQuery way of defining the methods (having a non-callback argument
> being last).
>
> I sat down and wrote up a quick jQuery.bind() but found one critical
> issue that was not resolved by the hitch/bind/fn.prototype.bind
> technique: You can't (easily) unbind a function that has a different
> scope defined.
>
> For example:
>
> function foo(){}
> .bind( "click", foo.bind(someObject) );
> .unbind( "click", foo /* errr.... we actually need to save the fn
> somewhere */ );
>
> jQuery has already solved this problem internally using our
> jQuery.event.proxy method - and, in fact, if I were to land a
> jQuery.bind() it would end up using jQuery.event.proxy(). But if you
> look at jQuery.event.proxy() you can see that, in reality, we could
> just be using that method and skip this whole dance entirely. For
> example (and this works today, in jQuery 1.3.2):
>
> function foo(){}
> .bind( "click", jQuery.event.proxy( foo, someObject ) );
> .unbind( "click", foo );
>
> Save for the sugar that hitch provides I can't see any reason to not
> just promote jQuery.event.proxy() to jQuery.proxy() and make it an
> officially supported part of the jQuery API.
>
> Filed: http://dev.jquery.com/ticket/5736
> Landed (in a branch, for review and further discussion):
> http://github.com/jquery/jquery/commit/66975de2d249643779e2b3daad0457f7f5f92508
>
> --John
>
> For fun, here is the jQuery.bind() that I quickly wrote (that DOESN'T
> use jQuery.proxy):
>
> diff --git a/src/core.js b/src/core.js
> index 944e8a9..1908123 100644
> --- a/src/core.js
> +++ b/src/core.js
> @@ -614,6 +614,20 @@ jQuery.extend({
>                return ret.concat.apply( [], ret );
>        },
>
> +       bind: function( scope, fn ) {
> +               if ( scope ) {
> +                       if ( typeof fn === "string" ) {
> +                               fn = scope[ fn ];
> +                       }
> +
> +                       if ( fn ) {
> +                               return function() {
> +                                       return fn.apply( scope, arguments );
> +                               };
> +                       }
> +               }
> +       },
> +
>        // Use of jQuery.browser is frowned upon.
>        // More details: http://docs.jquery.com/Utilities/jQuery.browser
>        browser: {
>
>
>
>
> On Tue, Dec 29, 2009 at 4:58 PM, Rick Waldron <waldron.r...@gmail.com> wrote:
>> This is exactly what I was getting at... With regard to event handler
>> .bind() and fn.bind()
>>
>> So far with my $.hitch tests, the one thing I dislike is the argument
>> structure. It does what it should but I'd much prefer
>> a function.prototype.bind() if given the choice.
>>
>>
>>
>> -- Sent from my Palm Prē
>> ________________________________
>> ajpiano wrote:
>>
>> I love the idea of extending scope manipulation to any function,
>> rather than only event handlers. Callbacks to ajax requests often
>> need a better scope than the XHR, and while I look forward to 1.4's
>> functionality for event handlers, it would really be a shame to
>> continue to force people to use non-jQuery solutions for full scope
>> manipulation.
>>
>> That said, and while I do love (and frequently recommend) $.hitch, I
>> prefer an approach more like Prototype or Underscore's that doesn't
>> involve passing so many strings.
>>
>> --adam
>>
>> On Dec 29, 3:45 pm, Peter Higgins <phigg...@gmail.com> wrote:
>>> It is a short-port of Dojo's dojo.hitch(). The only thing it doesn't do
>>> that Dojo's does is currying the arguments in the original hitched
>>> function, eg:
>>>
>>> // a bad example off the top of my head
>>> var x = $(".nodes");
>>> var clicker = $.hitch(x, "bind", "click");
>>>
>>> clicker(function(e){
>>>     // this just called $(.nodes").bind("click", arguments[0])
>>>
>>> });
>>>
>>> It would be another few bytes to support that. dojo.partial is equally
>>> as neat.
>>>
>>> Regards,
>>> Peter
>>>
>>>
>>>
>>> Rick Waldron wrote:
>>> > $.hitch() is a great "fn.bind()" solution, I still want to try a
>>> > variety of scope related tests, but so far its really solid. I love
>>> > the fact that you included the exception for a non existent  method, I
>>> > referred to Prototype's latest and there is no such check.
>>>
>>> > Hats off.
>>>
>>> > Rick
>>>
>>> > On Tue, Dec 29, 2009 at 12:40 PM, Rick Waldron <waldron.r...@gmail.com
>>> > <mailto:waldron.r...@gmail.com>> wrote:
>>>
>>> >     Agreed, that is slick. As soon as I get back to the office I'm
>>> >     going to test it, I look forward to this.
>>>
>>> >     -- Sent from my Palm Prē
>>>
>>> >
>>> > ------------------------------------------------------------------------
>>> >     aHeckman wrote:
>>>
>>> >     Yeah this looks good Peter. This should be in core IMHO.
>>>
>>> >     BTW, you're running for president? LOL
>>>
>>> >     On Dec 29, 9:24 am, Peter Higgins <phigg...@gmail.com
>>> >     <mailto:phigg...@gmail.com>> wrote:
>>> >     > ... this is why I keep suggesting making the bind functionality as
>>> >     > explicit function call, rather than hidden away in one or two
>>> > api's:
>>>
>>> >     >http://higginsforpresident.net/js/static/jq.hitch.js
>>>
>>> >     > It does not extend any native prototypes, is useful and a bit
>>> > magic
>>> >     > (with the string->method resolution).
>>>
>>> >     > Regards,
>>> >     > Peter
>>>
>>> >     > aHeckman wrote:
>>> >     > > I too feel relying on a function.prototype.bind implementation
>>> >     would
>>> >     > > be the most forward looking but I'm not sure that jives with the
>>> >     > > general approach of jQuery:
>>>
>>> >     > > jQuery doesn't extend Native.prototype.anything.
>>>
>>> >     > > On Dec 29, 1:12 am, Daniel Friesen <nadir.seen.f...@gmail.com
>>> >     <mailto:nadir.seen.f...@gmail.com>> wrote:
>>>
>>> >     > >> Rick Waldron wrote:
>>>
>>> >     > >>> Available, as in the "scope" argument is being retrofitted to
>>> > an
>>> >     > >>> existing function, and ONLY to that function.
>>>
>>> >     > >>>     I don't get what you are talking about a fn.bind()
>>> >     implementation in
>>> >     > >>>     jQuery, or what you mean by available in just one
>>> >     function though.
>>>
>>> >     > >>> Read ES5.
>>>
>>> >     > >>> function.prototype.bind()
>>>
>>> >     > >> I already read ES5, I use portions of ES5 in a number of js
>>> >     server-based
>>> >     > >> projects already.
>>>
>>> >     > >> However I don't get "ONLY" one function, since the whole point
>>> > of
>>> >     > >> .bind() is to bind a `this` onto ONE function with one call.
>>> >     It's not
>>> >     > >> bind otherwise.
>>>
>>> >     > >> So I don't see any limitation. Unless you are under the
>>> >     > >> misinterpretation that after you have called .bind() on one
>>> >     function you
>>> >     > >> have modified that function and bound it's `this`. .bind()
>>> >     doesn't
>>> >     > >> modify the function, it returns a new one.
>>> >     > >>  From ES5 15.3.4.5 Function.prototype.bind> The bind method
>>> >     takes one or more arguments, thisArg and (optionally)
>>>
>>> >     > >>> arg1, arg2, etc, and returns a *new*
>>> >     > >>> function object by performing the following steps:
>>>
>>> >     > >> So this is valid ES5 code.
>>>
>>> >     > >> "use strict";
>>> >     > >> var a = function() { alert(this); };
>>> >     > >> var a1 = a.bind("a");
>>> >     > >> var a2 = a.bind("b");
>>>
>>> >     > >> a(); // Alerts undefined
>>> >     > >> a1(); // Alerts "a"
>>> >     > >> a2(); // Alerts "b"
>>>
>>> >     > >>> On Mon, Dec 28, 2009 at 7:43 PM, Daniel Friesen
>>> >     > >>> <nadir.seen.f...@gmail.com <mailto:nadir.seen.f...@gmail.com>
>>> >     <mailto:nadir.seen.f...@gmail.com
>>> >     <mailto:nadir.seen.f...@gmail.com>>> wrote:
>>>
>>> >     > >>>     I made a post about how confusing people may find the
>>> >     name bind some
>>> >     > >>>     time ago. Suggested renaming bind to something like
>>> >     event, and keeping
>>> >     > >>>     bind as an alias of course. That was rejected.
>>>
>>> >     > >>>     I don't get what you are talking about a fn.bind()
>>> >     implementation in
>>> >     > >>>     jQuery, or what you mean by available in just one
>>> >     function though.
>>>
>>> >     > >>>     ~Daniel Friesen (Dantman, Nadir-Seen-Fire)
>>> >     > >>>     [http://daniel.friesen.name]
>>>
>>> >     > >>>     Rick Waldron wrote:
>>> >     > >>>     > John,
>>>
>>> >     > >>>     > While I'm glad to see a scope arg available, i still
>>> >     think this is
>>> >     > >>>     > negligent to the future of jQuery and ES standards. I
>>> >     really think a
>>> >     > >>>     > fn.bind() implementation would ideal (since it would be
>>> >     jQuery-wide
>>> >     > >>>     > and not just available in one function), but as I've
>>> >     noted in
>>> >     > >>>     the past
>>> >     > >>>     > and is exampled here, beginners may find this syntax a
>>> >     bit boggling:
>>>
>>> >     > >>>     > $(foo).bind('event', fn.bind(bar) );
>>>
>>> >     > >>>     > Rick
>>>
>>> >     > >> ~Daniel Friesen (Dantman, Nadir-Seen-Fire)
>>> >     [http://daniel.friesen.name]
>>>
>>> >     > > --
>>>
>>> >     > > 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 <mailto:jquery-dev@googlegroups.com>.
>>> >     > > To unsubscribe from this group, send email to
>>> >     jquery-dev+unsubscr...@googlegroups.com
>>> >     <mailto:jquery-dev%2bunsubscr...@googlegroups.com>.
>>> >     > > For more options, visit this group
>>> >     athttp://groups.google.com/group/jquery-dev?hl=en
>>> >     <http://groups.google.com/group/jquery-dev?hl=en>.
>>>
>>> >     --
>>>
>>> >     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
>>> >     <mailto:jquery-dev@googlegroups.com>.
>>> >     To unsubscribe from this group, send email to
>>> >     jquery-dev+unsubscr...@googlegroups.com
>>> >     <mailto:jquery-dev%2bunsubscr...@googlegroups.com>.
>>> >     For more options, visit this group at
>>> >    http://groups.google.com/group/jquery-dev?hl=en.
>>>
>>> > --
>>>
>>> > You received this message because you are subscribed to the Google
>>> > Groups "jQuery Development" group.
>>> > To post to this group, send email to jquery-...@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.
>>
>> --
>>
>> You received this message because you are subscribed to the Google Groups
>> "jQuery Development" group.
>> To post to this group, send email to jquery-...@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.
>>
>>
>> --
>>
>> You received this message because you are subscribed to the Google Groups
>> "jQuery Development" group.
>> To post to this group, send email to jquery-...@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.
>>
>

--

You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-...@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