Modify ↓
Ticket #5411 (closed bug: invalid)
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
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
Note: See
TracTickets for help on using
tickets.

The test case contained incorrect code to achieve the desired result.
Correct: http://jsfiddle.net/rwaldron/D2ECC/1/