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

Commit 937caab

Browse files
committed
feat(jqLite): provide support for element.one()
1 parent 3fc8017 commit 937caab

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/jqLite.js

+14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* - [`next()`](http://api.jquery.com/next/) - Does not support selectors
5555
* - [`on()`](http://api.jquery.com/on/) - Does not support namespaces, selectors or eventData
5656
* - [`off()`](http://api.jquery.com/off/) - Does not support namespaces or selectors
57+
* - [`one()`](http://api.jquery.com/one/) - Does not support namespaces or selectors
5758
* - [`parent()`](http://api.jquery.com/parent/) - Does not support selectors
5859
* - [`prepend()`](http://api.jquery.com/prepend/)
5960
* - [`prop()`](http://api.jquery.com/prop/)
@@ -744,6 +745,19 @@ forEach({
744745

745746
off: jqLiteOff,
746747

748+
one: function(element, type, fn) {
749+
element = jqLite(element);
750+
751+
//add the listener twice so that when it is called
752+
//you can remove the original function and still be
753+
//able to call element.off(ev, fn) normally
754+
element.on(type, function onFn() {
755+
element.off(type, fn);
756+
element.off(type, onFn);
757+
});
758+
element.on(type, fn);
759+
},
760+
747761
replaceWith: function(element, replaceNode) {
748762
var index, parent = element.parentNode;
749763
jqLiteDealoc(element);

test/jqLiteSpec.js

+57
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,63 @@ describe('jqLite', function() {
11771177
}
11781178
});
11791179

1180+
describe('one', function() {
1181+
1182+
it('should only fire the callback once', function() {
1183+
var element = jqLite(a);
1184+
var spy = jasmine.createSpy('click');
1185+
1186+
element.one('click', spy);
1187+
1188+
browserTrigger(element, 'click');
1189+
expect(spy).toHaveBeenCalledOnce();
1190+
1191+
browserTrigger(element, 'click');
1192+
expect(spy).toHaveBeenCalledOnce();
1193+
});
1194+
1195+
it('should deregister when off is called', function() {
1196+
var element = jqLite(a);
1197+
var spy = jasmine.createSpy('click');
1198+
1199+
element.one('click', spy);
1200+
element.off('click', spy);
1201+
1202+
browserTrigger(element, 'click');
1203+
expect(spy).not.toHaveBeenCalled();
1204+
});
1205+
1206+
it('should return the same event object just as on() does', function() {
1207+
var element = jqLite(a);
1208+
var eventA, eventB;
1209+
element.on('click', function(event) {
1210+
eventA = event;
1211+
});
1212+
element.one('click', function(event) {
1213+
eventB = event;
1214+
});
1215+
1216+
browserTrigger(element, 'click');
1217+
expect(eventA).toEqual(eventB);
1218+
});
1219+
1220+
it('should not remove other event handlers of the same type after execution', function() {
1221+
var element = jqLite(a);
1222+
var calls = [];
1223+
element.one('click', function(event) {
1224+
calls.push('one');
1225+
});
1226+
element.on('click', function(event) {
1227+
calls.push('on');
1228+
});
1229+
1230+
browserTrigger(element, 'click');
1231+
browserTrigger(element, 'click');
1232+
1233+
expect(calls).toEqual(['one','on','on']);
1234+
});
1235+
});
1236+
11801237

11811238
describe('replaceWith', function() {
11821239
it('should replaceWith', function() {

0 commit comments

Comments
 (0)