Bug Tracker

Modify

Ticket #2249 (closed enhancement: fixed)

Opened 5 years ago

Last modified 5 years ago

Addition of "once" event handler

Reported by: bfattori Owned by:
Priority: minor Milestone: 1.2.4
Component: event Version: 1.2.2
Keywords: once,one,bind,events Cc:
Blocking: Blocked by:

Description

The following code will assign an event, and then unbind that event immediately after assignment. Similar in effect to "one", however "one" will unbind all events bound to the type. "once" will only unbind the event passed.

/**
 * Similar in effect to using one() to bind an event to fire once and be
 * removed, however this will only remove the handler you assign.  Any
 * handlers already assigned to the event will not be removed as one() does.
 */
once: function(type, data, fn) {
	return this.each(function(){
		jQuery.event.add( this, type, function(event) {
			jQuery(this).unbind(event, arguments.callee);
			return (fn || data).apply( this, arguments);
		}, data);
	});
}

Attachments

one.diff Download (454 bytes) - added by flesler 5 years ago.
bfattori's proposal
one+proxy.diff Download (1.6 KB) - added by flesler 5 years ago.
Should fix the proxy issue, and adds a generic function for jQuery to reuse. Implemented it in jQuery.event.add.
one+diff+toggle.diff Download (2.8 KB) - added by flesler 5 years ago.
$.proxy moved to $.event.proxy, merged with the 'toggle-with-n-functions' feature. proxy is implemented in toggle as well.

Change History

comment:1 Changed 5 years ago by bfattori

Crap... I hate that I cannot edit my ticket! It doesn't unbind after assignment, but after execution

Sheesh... =)

comment:2 Changed 5 years ago by flesler

I think this is actually a bug of jQuery, IMO one should only unbind the recently bound function.

Changed 5 years ago by flesler

bfattori's proposal

comment:3 Changed 5 years ago by telega

I have one more suggestion about "one" function: currently it isn't possible to unbind function bound using one(), i.e. unbind in the following code doesn't work:

  function test() {
    alert("test");
  }
  $(document).one("click", test);
  $(document).unbind("click", test);

I guess it would be good to have the ability to unbind "one" event handlers.

comment:4 Changed 5 years ago by flesler

It might also be used for jQuery.fn._toggle, mapping both (or more) functions, to the bound click handler.

Changed 5 years ago by flesler

Should fix the proxy issue, and adds a generic function for jQuery to reuse. Implemented it in jQuery.event.add.

Changed 5 years ago by flesler

$.proxy moved to $.event.proxy, merged with the 'toggle-with-n-functions' feature. proxy is implemented in toggle as well.

comment:5 Changed 5 years ago by flesler

Fixed and implemented $.event.proxy at [5352]

comment:6 Changed 5 years ago by joern

  • Status changed from new to closed
  • Resolution set to fixed

Fixed in [5352].

comment:7 Changed 5 years ago by flesler

  • Milestone changed from 1.2.3 to 1.2.4

comment:8 follow-up: ↓ 9 Changed 5 years ago by aheimlich

It seems that the fix for this didn't actually make it into v1.2.4, so I'd suggest changing the milestone to avoid confusing others like myself (who actually that this was fixed in v1.2.4).

comment:9 in reply to: ↑ 8 Changed 5 years ago by aheimlich

Replying to aheimlich:

(who actually that this was fixed in v1.2.4).

duurrrr....

That should have been: (who actually thought that this was fixed in v1.2.4).

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.