Permalink
Browse files

added scope binding and argument passing to publishers

  • Loading branch information...
1 parent a181594 commit f03df0b80e8ddd95f6e19b5736e936357246ebe7 @llemirtrauts committed Oct 29, 2011
Showing with 81 additions and 10 deletions.
  1. +65 −4 src-test/observabletest.js
  2. +16 −6 src/sm.js
View
@@ -18,7 +18,7 @@ TestCase( "Observable subscribe test case", {
this.observable.sub("namedevent", handler);
- assertEquals(handler, this.observable.events["namedevent"][0]);
+ assertEquals(handler, this.observable.events["namedevent"][0].func);
},
"test throws TypeError if handler is not a function": function() {
@@ -40,8 +40,8 @@ TestCase( "Observable subscribe test case", {
this.observable.sub("namedevent2", handler3);
this.observable.sub("namedevent2", handler4);
this.observable.unSub();
-
- console.log(this.observable.events);
+
+ //this needs an assert
},
"test specific named events can be removed": function() {
@@ -85,10 +85,71 @@ TestCase( "Observable publish test case", {
setUp: function() {
this.observable = SM.object(SM.event.observable);
- this.observer = {};
},
tearDown: function() {
+ },
+
+ "test subscriber function is called on publish": function() {
+ var handler = function(){
+ handler.called = true;
+ };
+
+ this.observable.sub("namedevent", handler);
+ this.observable.pub("namedevent");
+
+ assertTrue(handler.called);
+ },
+
+ "test all subscriber functions are called on publish": function() {
+ var handlers = [
+ function(){handlers[0].called = true},
+ function(){handlers[1].called = true},
+ function(){handlers[2].called = true},
+ function(){handlers[3].called = true},
+ ];
+ this.observable.sub("namedevent", handlers[0]);
+ this.observable.sub("namedevent", handlers[1]);
+ this.observable.sub("namedevent1", handlers[2]);
+ this.observable.sub("namedevent1", handlers[3]);
+ this.observable.pub("namedevent");
+ this.observable.pub("namedevent1");
+
+ for(var i; i < handlers.length; i++) {
+ assertTrue(handlers[i].called);
+ }
+
+ },
+
+ "test subscribers are called in the required scope": function() {
+ var object = {
+ whoareyou: "batman"
+ };
+
+ function handler() {
+ handler.whoareyou = this.whoareyou;
+ }
+ this.observable.sub("namedevent", handler, object);
+ this.observable.pub("namedevent");
+
+ assertEquals("batman", handler.whoareyou);
+ },
+
+ "test subscribers receive arguments from publishers": function() {
+ var handler = function(){
+ handler.args = arguments;
+ };
+ this.observable.sub("namedevent", handler);
+ this.observable.pub("namedevent", "one", "two", "three");
+
+ assertEquals(3, handler.args.length);
+ assertEquals("one", handler.args[0]);
+ assertEquals("two", handler.args[1]);
+ assertEquals("three", handler.args[2]);
+ },
+
+ "test arguments are passed to handler function on event publish": function() {
+ var handler = function(){};
}
} );
View
@@ -146,18 +146,27 @@ if( typeof SM == "undefined" || !SM ) {
SM.event.observable = {
+ /**
+ * Fires events. Callbacks are passed the same arguments
+ * passed to pub apart from the event name
+ */
pub: function(name) {
- var event;
+ var event, events, handler, args;
+
+ args = Array.prototype.slice.call(arguments, 1, arguments.length);
+ events = this.events;
//ensure that the event object and named event array exist
- if(this.events && event = this.events[name]) {
+ if(events && events[name]) {
+ event = events[name];
for(var i = 0, l = event.length; i < l; i++) {
- event[i]();
+ handler = event[i];
+ handler.func.apply(handler.context || this, args);
}
}
},
- sub: function(name, handler) {
+ sub: function(name, handler, context) {
var events = this.events;
if(typeof handler != "function") {
@@ -171,7 +180,7 @@ if( typeof SM == "undefined" || !SM ) {
if(!events[name]) {
events[name] = [];
}
- events[name].push(handler);
+ events[name].push({func: handler, context: context});
},
unSub: function(name, handler) {
@@ -182,8 +191,9 @@ if( typeof SM == "undefined" || !SM ) {
this.events[name] = null;
} else {
for(var i = 0, l = this.events[name].length; i < l; i++) {
- if(this.events[name][i] === handler) {
+ if(this.events[name][i].func === handler) {
this.events[name].splice(i, 1);
+ break;
}
}
}

0 comments on commit f03df0b

Please sign in to comment.