add basic pub/sub functionality to WidgetCollection

This commit is contained in:
Dan Hansen 2011-04-15 18:29:48 -05:00
parent c0cd8fafff
commit 296824c92b
2 changed files with 32 additions and 1 deletions

View file

@ -13,6 +13,7 @@
Diaspora.WidgetCollection = function() {
this.initialized = false;
this.collection = { };
this.eventsContainer = $({});
};
Diaspora.WidgetCollection.prototype.add = function(widgetId, widget) {
@ -36,8 +37,16 @@
}
};
Diaspora.widgets = new Diaspora.WidgetCollection();
Diaspora.WidgetCollection.prototype.subscribe = function(id, callback) {
this.eventsContainer.bind(id, callback);
};
Diaspora.WidgetCollection.prototype.publish = function(id) {
this.eventsContainer.trigger(id);
};
Diaspora.widgets = new Diaspora.WidgetCollection();
window.Diaspora = Diaspora;
})();

View file

@ -51,6 +51,28 @@ describe("Diaspora", function() {
expect(widgets.initialized).toBeTruthy();
});
});
describe("subscribe", function() {
it("subscribes to an event specified by an id", function() {
expect(widgets.eventsContainer.data("events")).not.toBeDefined();
widgets.subscribe("testing/event", function() { });
expect(widgets.eventsContainer.data("events")["testing/event"]).toBeDefined();
});
});
describe("publish", function() {
it("triggers events that are related to the specified id", function() {
var called = false;
widgets.subscribe("testing/event", function() {
called = true;
});
widgets.publish("testing/event");
expect(called).toBeTruthy();
});
});
});
});
});