Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

allow an event callback to unbind itself (thanks @aron)

  • Loading branch information...
commit bbd85bb21a7fab0975821ee8b342177ea9bede8d 1 parent 5ea920a
@maccman maccman authored
Showing with 28 additions and 1 deletion.
  1. +3 −1 spine.js
  2. +25 −0 test/specs/events.js
View
4 spine.js
@@ -62,7 +62,7 @@
var list, calls, i, l;
if (!(calls = this._callbacks)) return this;
- if (!(list = this._callbacks[ev])) return this;
+ if (!(list = calls[ev])) return this;
if ( !callback ) {
delete this._callbacks[ev];
@@ -71,7 +71,9 @@
for (i = 0, l = list.length; i < l; i++)
if (callback === list[i]) {
+ list = list.slice();
list.splice(i, 1);
+ calls[ev] = list;
break;
}
View
25 test/specs/events.js
@@ -42,6 +42,31 @@ describe("Events", function(){
expect(spy).not.toHaveBeenCalled();
});
+ it("should allow a callback unbind itself", function(){
+ var a = jasmine.createSpy("a");
+ var b = jasmine.createSpy("b");
+ var c = jasmine.createSpy("c");
+
+ b.andCallFake(function () {
+ EventTest.unbind("once", b);
+ });
+
+ EventTest.bind("once", a);
+ EventTest.bind("once", b);
+ EventTest.bind("once", c);
+ EventTest.trigger("once");
+
+ expect(a).toHaveBeenCalled();
+ expect(b).toHaveBeenCalled();
+ expect(c).toHaveBeenCalled();
+
+ EventTest.trigger("once");
+
+ expect(a.callCount).toBe(2);
+ expect(b.callCount).toBe(1);
+ expect(c.callCount).toBe(2);
+ });
+
it("can cancel propogation", function(){
EventTest.bind("motherio", function(){ return false });
EventTest.bind("motherio", spy);
Please sign in to comment.
Something went wrong with that request. Please try again.