Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 01d81cd

Browse files
fix(jqLite): allow triggerHandler() to accept custom event
In some scenarios you want to be able to specify properties on the event that is passed to the event handler. JQuery does this by overloading the first parameter (`eventName`). If it is an object with a `type` property then we assume that it must be a custom event. In this case the custom event must provide the `type` property which is the name of the event to be triggered. `triggerHandler` will continue to provide dummy default functions for `preventDefault()`, `isDefaultPrevented()` and `stopPropagation()` but you may override these with your own versions in your custom object if you wish. In addition the commit provides some performance and memory usage improvements by only creating objects and doing work that is necessary. This commit also renames the parameters inline with jQuery. Closes #8469
1 parent 37ba3b9 commit 01d81cd

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

src/jqLite.js

+31-20
Original file line numberDiff line numberDiff line change
@@ -947,26 +947,37 @@ forEach({
947947

948948
clone: jqLiteClone,
949949

950-
triggerHandler: function(element, eventName, eventData) {
951-
// Copy event handlers in case event handlers array is modified during execution.
952-
var eventFns = (jqLiteExpandoStore(element, 'events') || {})[eventName],
953-
eventFnsCopy = shallowCopy(eventFns || []);
954-
955-
eventData = eventData || [];
956-
957-
var event = [{
958-
preventDefault: function() {
959-
this.defaultPrevented = true;
960-
},
961-
isDefaultPrevented: function() {
962-
return this.defaultPrevented === true;
963-
},
964-
stopPropagation: noop
965-
}];
966-
967-
forEach(eventFnsCopy, function(fn) {
968-
fn.apply(element, event.concat(eventData));
969-
});
950+
triggerHandler: function(element, event, extraParameters) {
951+
952+
var dummyEvent, eventFnsCopy, handlerArgs;
953+
var eventName = event.type || event;
954+
var eventFns = (jqLiteExpandoStore(element, 'events') || {})[eventName];
955+
956+
if (eventFns) {
957+
958+
// Create a dummy event to pass to the handlers
959+
dummyEvent = {
960+
preventDefault: function() { this.defaultPrevented = true; },
961+
isDefaultPrevented: function() { return this.defaultPrevented === true; },
962+
stopPropagation: noop,
963+
type: eventName,
964+
target: element
965+
};
966+
967+
// If a custom event was provided then extend our dummy event with it
968+
if (event.type) {
969+
dummyEvent = extend(dummyEvent, event);
970+
}
971+
972+
// Copy event handlers in case event handlers array is modified during execution.
973+
eventFnsCopy = shallowCopy(eventFns);
974+
handlerArgs = extraParameters ? [dummyEvent].concat(extraParameters) : [dummyEvent];
975+
976+
forEach(eventFnsCopy, function(fn) {
977+
fn.apply(element, handlerArgs);
978+
});
979+
980+
}
970981
}
971982
}, function(fn, name){
972983
/**

test/jqLiteSpec.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -1718,9 +1718,11 @@ describe('jqLite', function() {
17181718
element.triggerHandler('click');
17191719
event = pokeSpy.mostRecentCall.args[0];
17201720
expect(event.preventDefault).toBeDefined();
1721+
expect(event.target).toEqual(element[0]);
1722+
expect(event.type).toEqual('click');
17211723
});
17221724

1723-
it('should pass data as an additional argument', function() {
1725+
it('should pass extra parameters as an additional argument', function() {
17241726
var element = jqLite('<a>poke</a>'),
17251727
pokeSpy = jasmine.createSpy('poke'),
17261728
data;
@@ -1765,6 +1767,24 @@ describe('jqLite', function() {
17651767
expect(clickOnceSpy).toHaveBeenCalledOnce();
17661768
expect(clickSpy.callCount).toBe(2);
17671769
});
1770+
1771+
it("should accept a custom event instead of eventName", function() {
1772+
var element = jqLite('<a>poke</a>'),
1773+
pokeSpy = jasmine.createSpy('poke'),
1774+
customEvent = {
1775+
type: 'click',
1776+
someProp: 'someValue'
1777+
},
1778+
actualEvent;
1779+
1780+
element.on('click', pokeSpy);
1781+
element.triggerHandler(customEvent);
1782+
actualEvent = pokeSpy.mostRecentCall.args[0];
1783+
expect(actualEvent.preventDefault).toBeDefined();
1784+
expect(actualEvent.someProp).toEqual('someValue');
1785+
expect(actualEvent.target).toEqual(element[0]);
1786+
expect(actualEvent.type).toEqual('click');
1787+
});
17681788
});
17691789

17701790

0 commit comments

Comments
 (0)