mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
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.
This commit is contained in:
parent
e249502880
commit
650fd933df
2 changed files with 37 additions and 1 deletions
|
|
@ -54,6 +54,7 @@
|
|||
* - [replaceWith()](http://api.jquery.com/replaceWith/)
|
||||
* - [text()](http://api.jquery.com/text/)
|
||||
* - [toggleClass()](http://api.jquery.com/toggleClass/)
|
||||
* - [triggerHandler()](http://api.jquery.com/triggerHandler/) - Doesn't pass native event objects to handlers.
|
||||
* - [unbind()](http://api.jquery.com/unbind/)
|
||||
* - [val()](http://api.jquery.com/val/)
|
||||
* - [wrap()](http://api.jquery.com/wrap/)
|
||||
|
|
@ -728,7 +729,15 @@ forEach({
|
|||
return element.getElementsByTagName(selector);
|
||||
},
|
||||
|
||||
clone: JQLiteClone
|
||||
clone: JQLiteClone,
|
||||
|
||||
triggerHandler: function(element, eventName) {
|
||||
var eventFns = (JQLiteExpandoStore(element, 'events') || {})[eventName];
|
||||
|
||||
forEach(eventFns, function(fn) {
|
||||
fn.call(element, null);
|
||||
});
|
||||
}
|
||||
}, function(fn, name){
|
||||
/**
|
||||
* chaining functions
|
||||
|
|
|
|||
|
|
@ -1091,6 +1091,33 @@ describe('jqLite', function() {
|
|||
});
|
||||
|
||||
|
||||
describe('triggerHandler', function() {
|
||||
it('should trigger all registered handlers for an event', function() {
|
||||
var element = jqLite('<span>poke</span>'),
|
||||
pokeSpy = jasmine.createSpy('poke'),
|
||||
clickSpy1 = jasmine.createSpy('clickSpy1'),
|
||||
clickSpy2 = jasmine.createSpy('clickSpy2');
|
||||
|
||||
element.bind('poke', pokeSpy);
|
||||
element.bind('click', clickSpy1);
|
||||
element.bind('click', clickSpy2);
|
||||
|
||||
expect(pokeSpy).not.toHaveBeenCalled();
|
||||
expect(clickSpy1).not.toHaveBeenCalled();
|
||||
expect(clickSpy2).not.toHaveBeenCalled();
|
||||
|
||||
element.triggerHandler('poke');
|
||||
expect(pokeSpy).toHaveBeenCalledOnce();
|
||||
expect(clickSpy1).not.toHaveBeenCalled();
|
||||
expect(clickSpy2).not.toHaveBeenCalled();
|
||||
|
||||
element.triggerHandler('click');
|
||||
expect(clickSpy1).toHaveBeenCalledOnce();
|
||||
expect(clickSpy2).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('camelCase', function() {
|
||||
|
||||
it('should leave non-dashed strings alone', function() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue