-
Notifications
You must be signed in to change notification settings - Fork 20.5k
More generic implementation of jQuery.fn[bind,one,unbind] #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This commit genericizes the implementation of the core jQuery.fn event bind/unbind functions so that they may be called in the context of any jQuery-like object. This can be useful for 3rd parties who wish to use jQuery's events subsystem for arbitrary objects in an Array.
It's a pretty simple change, but I'd like to know more about how this is being used since it's implying that we will need to document and support this into the future. |
The second commit (cd59836) demonstrates the typical use case for this style of implementation. In this case, instead of requiring another trip through the main Ideally (and this is just pure speculation, not suggesting this should happen right now) all of the methods in |
I guess the question I should have asked was not "how" but "why". What is the need for this? If we put it in, how will it be used and how are we not satisfying that need today? Are you saying it's a performance optimization? |
It's mainly an improvement in flexibility of the prototype (although there is one less function call as well). Right now, in order to effectively use any of the methods on |
Hmm, interesting idea. If there is no immediate need, I think it's worth considering in more detail and tackling all of this as an enhancement in a future release. Would you like to create a ticket and reference this pull request? |
@@ -904,7 +904,7 @@ jQuery.each(["bind", "one"], function( i, name ) { | |||
// Handle object literals | |||
if ( typeof type === "object" ) { | |||
for ( var key in type ) { | |||
this[ name ](key, data, type[key], fn); | |||
jQuery.fn[ name ].call( this, key, data, type[key] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've left out the event callback here. Also I'm not sure I see the advantage of switching to the generic prototype over using the prototype on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure that extra fn
is actually a typo in the original source. After all, jQuery.fn.bind
and jQuery.fn.one
only take three arguments each. On that line, they're being called with four. Hence, it was omitted intentionally. And all tests pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, looking closer that would be for type/function key/value pairs and fn would not be used in that case.
Per the ticket we've decided not to make these changes. If you'd like to advocate the issue further, make a post on forum.jquery.com in the Developing jQuery Core section. |
This commit makes the implementation of jQuery.fn.bind, jQuery.fn.one, and jQuery.fn.unbind more generic so that they may be called in the context of a jQuery-like object, as well as an actual jQuery object. This can be useful for 3rd parties who wish to use these functions to bind/unbind events for any set of arbitrary objects in an Array.