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

Commit 650fd93

Browse files
committed
feat(jqLite): add triggerHandler()
we need triggerHandler() to become jQuery 1.8.x compatible. this is not fully featured triggerHandler() implementation - it doesn't bother creating new DOM events and passing them into the event handlers. this is intentional, we don't need access to the native DOM event for our own purposes and creating these event objects is really tricky.
1 parent e249502 commit 650fd93

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/jqLite.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* - [replaceWith()](http://api.jquery.com/replaceWith/)
5555
* - [text()](http://api.jquery.com/text/)
5656
* - [toggleClass()](http://api.jquery.com/toggleClass/)
57+
* - [triggerHandler()](http://api.jquery.com/triggerHandler/) - Doesn't pass native event objects to handlers.
5758
* - [unbind()](http://api.jquery.com/unbind/)
5859
* - [val()](http://api.jquery.com/val/)
5960
* - [wrap()](http://api.jquery.com/wrap/)
@@ -728,7 +729,15 @@ forEach({
728729
return element.getElementsByTagName(selector);
729730
},
730731

731-
clone: JQLiteClone
732+
clone: JQLiteClone,
733+
734+
triggerHandler: function(element, eventName) {
735+
var eventFns = (JQLiteExpandoStore(element, 'events') || {})[eventName];
736+
737+
forEach(eventFns, function(fn) {
738+
fn.call(element, null);
739+
});
740+
}
732741
}, function(fn, name){
733742
/**
734743
* chaining functions

test/jqLiteSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,33 @@ describe('jqLite', function() {
10911091
});
10921092

10931093

1094+
describe('triggerHandler', function() {
1095+
it('should trigger all registered handlers for an event', function() {
1096+
var element = jqLite('<span>poke</span>'),
1097+
pokeSpy = jasmine.createSpy('poke'),
1098+
clickSpy1 = jasmine.createSpy('clickSpy1'),
1099+
clickSpy2 = jasmine.createSpy('clickSpy2');
1100+
1101+
element.bind('poke', pokeSpy);
1102+
element.bind('click', clickSpy1);
1103+
element.bind('click', clickSpy2);
1104+
1105+
expect(pokeSpy).not.toHaveBeenCalled();
1106+
expect(clickSpy1).not.toHaveBeenCalled();
1107+
expect(clickSpy2).not.toHaveBeenCalled();
1108+
1109+
element.triggerHandler('poke');
1110+
expect(pokeSpy).toHaveBeenCalledOnce();
1111+
expect(clickSpy1).not.toHaveBeenCalled();
1112+
expect(clickSpy2).not.toHaveBeenCalled();
1113+
1114+
element.triggerHandler('click');
1115+
expect(clickSpy1).toHaveBeenCalledOnce();
1116+
expect(clickSpy2).toHaveBeenCalledOnce();
1117+
});
1118+
});
1119+
1120+
10941121
describe('camelCase', function() {
10951122

10961123
it('should leave non-dashed strings alone', function() {

0 commit comments

Comments
 (0)