initial implementation of events#once
This commit is contained in:
parent
54b2887667
commit
e41e8a886a
@ -245,4 +245,66 @@ describe('Events', function() {
|
||||
expect(spy3.called).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#once', function(){
|
||||
it('removes event listeners after first fire', function() {
|
||||
var obj = new Klass(),
|
||||
spy = new sinon.spy();
|
||||
|
||||
obj.once('test', spy);
|
||||
obj.fire('test');
|
||||
|
||||
expect(spy.called).to.be(true);
|
||||
|
||||
obj.fire('test');
|
||||
|
||||
expect(spy.callCount).to.be.lessThan(2);
|
||||
});
|
||||
|
||||
it('works with object hash', function() {
|
||||
var obj = new Klass(),
|
||||
spy = new sinon.spy(),
|
||||
otherSpy = new sinon.spy();
|
||||
|
||||
obj.once({
|
||||
test: spy,
|
||||
otherTest: otherSpy
|
||||
});
|
||||
|
||||
obj.fire('test');
|
||||
obj.fire('otherTest');
|
||||
|
||||
expect(spy.called).to.be(true);
|
||||
expect(otherSpy.called).to.be(true);
|
||||
|
||||
obj.fire('test');
|
||||
obj.fire('otherTest');
|
||||
|
||||
expect(spy.callCount).to.be.lessThan(2);
|
||||
expect(otherSpy.callCount).to.be.lessThan(2);
|
||||
});
|
||||
|
||||
it('only removes the fired event handler', function(){
|
||||
var obj = new Klass(),
|
||||
spy = new sinon.spy(),
|
||||
otherSpy = new sinon.spy();
|
||||
|
||||
obj.once({
|
||||
test: spy,
|
||||
otherTest: otherSpy
|
||||
});
|
||||
|
||||
obj.fire('test');
|
||||
|
||||
expect(spy.called).to.be(true);
|
||||
expect(otherSpy.called).to.be(false);
|
||||
|
||||
obj.fire('otherTest');
|
||||
|
||||
expect(otherSpy.called).to.be(true);
|
||||
|
||||
expect(spy.callCount).to.be.lessThan(2);
|
||||
expect(otherSpy.callCount).to.be.lessThan(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -148,6 +148,29 @@ L.Mixin.Events = {
|
||||
}
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
once: function(types, fn, context) {
|
||||
handlerFor = function(fn, type, context) {
|
||||
var handler = function() {
|
||||
this.removeEventListener(type, fn, context);
|
||||
this.removeEventListener(type, handler, context);
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
if (typeof types === 'object') {
|
||||
for (type in types) {
|
||||
if (types.hasOwnProperty(type)) {
|
||||
this.addEventListener(type, types[type], fn);
|
||||
this.addEventListener(type, handlerFor(types[type], type, fn), fn);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
this.addEventListener(types, fn, context);
|
||||
return this.addEventListener(types, handlerFor(fn, types, context), context);
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user