Bug Tracker

Modify

Ticket #5411 (closed bug: invalid)

Opened 4 years ago

Last modified 3 years ago

Cannot use prototype methods for event handler

Reported by: dinoboff Owned by: brandon
Priority: low Milestone: 1.4
Component: event Version: 1.3.2
Keywords: Cc: dinoboff@…
Blocking: Blocked by:

Description

Here is the test:

test("Use prototype method as event handler", function(){
	var $elem = $('#swipeTarget'),
		SomeConstructor = function($elem) {
			$elem.bind('test', this, this.handler);
		};
		
	SomeConstructor.prototype = {
		handler: function() {
                        var that = event.data;
                        ok(true, "Called.");}
	};
	
	expect(2);
	obj1 = new SomeConstructor($elem);
	obj2 = new SomeConstructor($elem);
	$elem.trigger('test');
});

The test will fail. Only obj2.handler will be called.

jQuery tracks event handler (for unbind) by adding them an unique guid property, and only one guid / element / event can be registered. In this case obj1.handler and obj2.handler are the same function and share the same guid.

Change History

comment:1 Changed 3 years ago by rwaldron

  • Priority changed from major to low
  • Status changed from new to closed
  • Resolution set to invalid

The test case contained incorrect code to achieve the desired result.

Correct:  http://jsfiddle.net/rwaldron/D2ECC/1/

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.