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

Commit 6b7ddf4

Browse files
committed
feat(jqLite): add support for unbind()
supports these invocation types: - foo.unbind(); - foo.unbind('eventType'); - foo.unbind('eventType', fn); more info: http://api.jquery.com/unbind/
1 parent 8259f10 commit 6b7ddf4

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

src/jqLite.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* - [clone()](http://api.jquery.com/clone/)
3838
* - [css()](http://api.jquery.com/css/)
3939
* - [data()](http://api.jquery.com/data/)
40+
* - [eq()](http://api.jquery.com/eq/)
4041
* - [hasClass()](http://api.jquery.com/hasClass/)
4142
* - [parent()](http://api.jquery.com/parent/)
4243
* - [remove()](http://api.jquery.com/remove/)
@@ -46,7 +47,7 @@
4647
* - [replaceWith()](http://api.jquery.com/replaceWith/)
4748
* - [text()](http://api.jquery.com/text/)
4849
* - [trigger()](http://api.jquery.com/trigger/)
49-
* - [eq()](http://api.jquery.com/eq/)
50+
* - [unbind()](http://api.jquery.com/unbind/)
5051
*
5152
* ## In addtion to the above, Angular privides an additional method to both jQuery and jQuery lite:
5253
*
@@ -401,6 +402,25 @@ forEach({
401402
});
402403
},
403404

405+
unbind: function(element, type, fn) {
406+
var bind = JQLiteData(element, 'bind');
407+
if (!bind) return; //no listeners registered
408+
409+
if (isUndefined(type)) {
410+
forEach(bind, function(eventHandler, type) {
411+
removeEventListenerFn(element, type, eventHandler);
412+
delete bind[type];
413+
});
414+
} else {
415+
if (isUndefined(fn)) {
416+
removeEventListenerFn(element, type, bind[type]);
417+
delete bind[type];
418+
} else {
419+
angularArray.remove(bind[type].fns, fn);
420+
}
421+
}
422+
},
423+
404424
replaceWith: function(element, replaceNode) {
405425
var index, parent = element.parentNode;
406426
JQLiteDealoc(element);

test/jqLiteSpec.js

+96
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,102 @@ describe('jqLite', function(){
450450
});
451451

452452

453+
describe('unbind', function() {
454+
it('should do nothing when no listener was registered with bound', function() {
455+
var aElem = jqLite(a);
456+
457+
aElem.unbind();
458+
aElem.unbind('click');
459+
aElem.unbind('click', function() {});
460+
});
461+
462+
463+
it('should deregister all listeners', function() {
464+
var aElem = jqLite(a),
465+
clickSpy = jasmine.createSpy('click'),
466+
mouseoverSpy = jasmine.createSpy('mouseover');
467+
468+
aElem.bind('click', clickSpy);
469+
aElem.bind('mouseover', mouseoverSpy);
470+
471+
browserTrigger(a, 'click');
472+
expect(clickSpy).toHaveBeenCalledOnce();
473+
browserTrigger(a, 'mouseover');
474+
expect(mouseoverSpy).toHaveBeenCalledOnce();
475+
476+
clickSpy.reset();
477+
mouseoverSpy.reset();
478+
479+
aElem.unbind();
480+
481+
browserTrigger(a, 'click');
482+
expect(clickSpy).not.toHaveBeenCalled();
483+
browserTrigger(a, 'mouseover');
484+
expect(mouseoverSpy).not.toHaveBeenCalled();
485+
});
486+
487+
488+
it('should deregister listeners for specific type', function() {
489+
var aElem = jqLite(a),
490+
clickSpy = jasmine.createSpy('click'),
491+
mouseoverSpy = jasmine.createSpy('mouseover');
492+
493+
aElem.bind('click', clickSpy);
494+
aElem.bind('mouseover', mouseoverSpy);
495+
496+
browserTrigger(a, 'click');
497+
expect(clickSpy).toHaveBeenCalledOnce();
498+
browserTrigger(a, 'mouseover');
499+
expect(mouseoverSpy).toHaveBeenCalledOnce();
500+
501+
clickSpy.reset();
502+
mouseoverSpy.reset();
503+
504+
aElem.unbind('click');
505+
506+
browserTrigger(a, 'click');
507+
expect(clickSpy).not.toHaveBeenCalled();
508+
browserTrigger(a, 'mouseover');
509+
expect(mouseoverSpy).toHaveBeenCalledOnce();
510+
511+
mouseoverSpy.reset();
512+
513+
aElem.unbind('mouseover');
514+
browserTrigger(a, 'mouseover');
515+
expect(mouseoverSpy).not.toHaveBeenCalled();
516+
});
517+
518+
519+
it('should deregister specific listener', function() {
520+
var aElem = jqLite(a),
521+
clickSpy1 = jasmine.createSpy('click1'),
522+
clickSpy2 = jasmine.createSpy('click2');
523+
524+
aElem.bind('click', clickSpy1);
525+
aElem.bind('click', clickSpy2);
526+
527+
browserTrigger(a, 'click');
528+
expect(clickSpy1).toHaveBeenCalledOnce();
529+
expect(clickSpy2).toHaveBeenCalledOnce();
530+
531+
clickSpy1.reset();
532+
clickSpy2.reset();
533+
534+
aElem.unbind('click', clickSpy1);
535+
536+
browserTrigger(a, 'click');
537+
expect(clickSpy1).not.toHaveBeenCalled();
538+
expect(clickSpy2).toHaveBeenCalledOnce();
539+
540+
clickSpy2.reset();
541+
542+
aElem.unbind('click', clickSpy2);
543+
browserTrigger(a, 'click');
544+
expect(clickSpy2).not.toHaveBeenCalled();
545+
});
546+
});
547+
548+
453549
describe('replaceWith', function(){
454550
it('should replaceWith', function(){
455551
var root = jqLite('<div>').html('before-<div></div>after');

0 commit comments

Comments
 (0)