mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
feat(jqLite): provide support for element.one()
This commit is contained in:
parent
3fc8017119
commit
937caab647
2 changed files with 71 additions and 0 deletions
|
|
@ -54,6 +54,7 @@
|
|||
* - [`next()`](http://api.jquery.com/next/) - Does not support selectors
|
||||
* - [`on()`](http://api.jquery.com/on/) - Does not support namespaces, selectors or eventData
|
||||
* - [`off()`](http://api.jquery.com/off/) - Does not support namespaces or selectors
|
||||
* - [`one()`](http://api.jquery.com/one/) - Does not support namespaces or selectors
|
||||
* - [`parent()`](http://api.jquery.com/parent/) - Does not support selectors
|
||||
* - [`prepend()`](http://api.jquery.com/prepend/)
|
||||
* - [`prop()`](http://api.jquery.com/prop/)
|
||||
|
|
@ -744,6 +745,19 @@ forEach({
|
|||
|
||||
off: jqLiteOff,
|
||||
|
||||
one: function(element, type, fn) {
|
||||
element = jqLite(element);
|
||||
|
||||
//add the listener twice so that when it is called
|
||||
//you can remove the original function and still be
|
||||
//able to call element.off(ev, fn) normally
|
||||
element.on(type, function onFn() {
|
||||
element.off(type, fn);
|
||||
element.off(type, onFn);
|
||||
});
|
||||
element.on(type, fn);
|
||||
},
|
||||
|
||||
replaceWith: function(element, replaceNode) {
|
||||
var index, parent = element.parentNode;
|
||||
jqLiteDealoc(element);
|
||||
|
|
|
|||
|
|
@ -1177,6 +1177,63 @@ describe('jqLite', function() {
|
|||
}
|
||||
});
|
||||
|
||||
describe('one', function() {
|
||||
|
||||
it('should only fire the callback once', function() {
|
||||
var element = jqLite(a);
|
||||
var spy = jasmine.createSpy('click');
|
||||
|
||||
element.one('click', spy);
|
||||
|
||||
browserTrigger(element, 'click');
|
||||
expect(spy).toHaveBeenCalledOnce();
|
||||
|
||||
browserTrigger(element, 'click');
|
||||
expect(spy).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('should deregister when off is called', function() {
|
||||
var element = jqLite(a);
|
||||
var spy = jasmine.createSpy('click');
|
||||
|
||||
element.one('click', spy);
|
||||
element.off('click', spy);
|
||||
|
||||
browserTrigger(element, 'click');
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return the same event object just as on() does', function() {
|
||||
var element = jqLite(a);
|
||||
var eventA, eventB;
|
||||
element.on('click', function(event) {
|
||||
eventA = event;
|
||||
});
|
||||
element.one('click', function(event) {
|
||||
eventB = event;
|
||||
});
|
||||
|
||||
browserTrigger(element, 'click');
|
||||
expect(eventA).toEqual(eventB);
|
||||
});
|
||||
|
||||
it('should not remove other event handlers of the same type after execution', function() {
|
||||
var element = jqLite(a);
|
||||
var calls = [];
|
||||
element.one('click', function(event) {
|
||||
calls.push('one');
|
||||
});
|
||||
element.on('click', function(event) {
|
||||
calls.push('on');
|
||||
});
|
||||
|
||||
browserTrigger(element, 'click');
|
||||
browserTrigger(element, 'click');
|
||||
|
||||
expect(calls).toEqual(['one','on','on']);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('replaceWith', function() {
|
||||
it('should replaceWith', function() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue