add basic pub/sub functionality to WidgetCollection
This commit is contained in:
parent
c0cd8fafff
commit
296824c92b
2 changed files with 32 additions and 1 deletions
|
|
@ -13,6 +13,7 @@
|
||||||
Diaspora.WidgetCollection = function() {
|
Diaspora.WidgetCollection = function() {
|
||||||
this.initialized = false;
|
this.initialized = false;
|
||||||
this.collection = { };
|
this.collection = { };
|
||||||
|
this.eventsContainer = $({});
|
||||||
};
|
};
|
||||||
|
|
||||||
Diaspora.WidgetCollection.prototype.add = function(widgetId, widget) {
|
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;
|
window.Diaspora = Diaspora;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,28 @@ describe("Diaspora", function() {
|
||||||
expect(widgets.initialized).toBeTruthy();
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue