better implementation, more tests

This commit is contained in:
iirvine 2013-04-12 17:03:14 -07:00
parent e41e8a886a
commit e25f730b04
2 changed files with 83 additions and 17 deletions

View File

@ -251,7 +251,7 @@ describe('Events', function() {
var obj = new Klass(),
spy = new sinon.spy();
obj.once('test', spy);
obj.once('test', spy, obj);
obj.fire('test');
expect(spy.called).to.be(true);
@ -269,7 +269,7 @@ describe('Events', function() {
obj.once({
test: spy,
otherTest: otherSpy
});
}, obj);
obj.fire('test');
obj.fire('otherTest');
@ -306,5 +306,66 @@ describe('Events', function() {
expect(spy.callCount).to.be.lessThan(2);
expect(otherSpy.callCount).to.be.lessThan(2);
});
it('can be passed multiple types', function() {
var obj = new Klass(),
spy = new sinon.spy(),
otherSpy = new sinon.spy();
obj.once('test otherTest', spy, obj);
obj.fire('otherTest');
expect(spy.called).to.be(true);
obj.fire('test');
expect(spy.callCount).to.be.lessThan(2);
});
it('provides event object to listeners and executes with the correct context', function() {
var obj = new Klass(),
obj2 = new Klass(),
obj3 = new Klass(),
obj4 = new Klass(),
foo = new Klass();
function listener1(e) {
expect(e.type).to.eql('test');
expect(e.target).to.eql(obj);
expect(this).to.eql(obj);
expect(e.baz).to.eql(1);
}
function listener2(e) {
expect(e.type).to.eql('test');
expect(e.target).to.eql(obj2);
expect(this).to.eql(foo);
expect(e.baz).to.eql(2);
}
function listener3(e) {
expect(e.type).to.eql('test');
expect(e.target).to.eql(obj3);
expect(this).to.eql(obj3);
expect(e.baz).to.eql(3);
}
function listener4(e) {
expect(e.type).to.eql('test');
expect(e.target).to.eql(obj4);
expect(this).to.eql(foo);
expect(e.baz).to.eql(4);
}
obj.once('test', listener1);
obj2.once('test', listener2, foo);
obj3.once({ test: listener3 });
obj4.once({ test: listener4 }, foo);
obj.fireEvent('test', {baz: 1});
obj2.fireEvent('test', {baz: 2});
obj3.fireEvent('test', {baz: 3});
obj4.fireEvent('test', {baz: 4});
});
});
});

View File

@ -115,10 +115,7 @@ L.Mixin.Events = {
return this;
}
var event = L.Util.extend({}, data, {
type: type,
target: this
});
var event = L.Util.extend({}, data, { type: type, target: this });
var listeners, i, len, eventsObj, contextId;
@ -151,26 +148,34 @@ L.Mixin.Events = {
},
once: function(types, fn, context) {
handlerFor = function(fn, type, context) {
var handler = function() {
this.removeEventListener(type, fn, context);
this.removeEventListener(type, handler, context);
var once = function(fn) {
var done = false;
return function() {
if (done) return;
fn.apply(this, arguments);
fn = null;
done = true;
}
}
var handlerFor = L.bind(function(type, fn, context) {
var handler = once(function() {
this.removeEventListener(types, handler);
fn.apply(context || this, arguments);
});
return handler;
}
}, this);
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);
this.addEventListener(type, handlerFor(type, types[type], fn), fn);
}
}
return this;
}
this.addEventListener(types, fn, context);
return this.addEventListener(types, handlerFor(fn, types, context), context);
return this.addEventListener(types, handlerFor(types,fn,context), context);
}
};