Merge branch 'restructuring' of github.com:logsol/chuck.js into restructuring

This commit is contained in:
Jeena Paradies 2012-07-22 02:02:29 +02:00
commit b8e6a967ee
2 changed files with 24 additions and 23 deletions

41
app/Game/Server/NotificationCenter.js Normal file → Executable file
View file

@ -1,32 +1,30 @@
define(function() {
define(
function NotificationCenter() {
this.topics = {};
this.subUid = -1;
}
var NotificationCenter = {
topics: {},
subUid: -1
};
NotificationCenter.prototype.trigger = function(topic, args) {
if (!this.topics[topic]) {
NotificationCenter.trigger = function(topic, args) {
if (!NotificationCenter.topics[topic]) {
throw "No such topic " + topic + ". Could not trigger.";
}
var subscribers = this.topics[topic];
var subscribers = NotificationCenter.topics[topic];
var len = subscribers ? subscribers.length : 0;
while (len--) {
subscribers[len].func(topic, args);
}
return this;
}
NotificationCenter.prototype.on = function(topic, func) {
if (!this.topics[topic]) {
this.topics[topic] = [];
NotificationCenter.on = function(topic, func) {
if (!NotificationCenter.topics[topic]) {
NotificationCenter.topics[topic] = [];
}
var token = ( ++this.subUid ).toString();
this.topics[topic].push({
var token = ( ++NotificationCenter.subUid ).toString();
NotificationCenter.topics[topic].push({
token: token,
func: func
});
@ -34,19 +32,18 @@ define(function() {
return token;
}
NotificationCenter.prototype.off = function(token) {
NotificationCenter.off = function(token) {
for(var m in this.topics) {
if (this.topics[m]) {
for(var i = 0, j = this.topics[m].length; i < j; i++) {
if (this.topics[m][i].token === token) {
this.topics[m].splice(i, 1);
for(var m in NotificationCenter.topics) {
if (NotificationCenter.topics[m]) {
for(var i = 0, j = NotificationCenter.topics[m].length; i < j; i++) {
if (NotificationCenter.topics[m][i].token === token) {
NotificationCenter.topics[m].splice(i, 1);
return token;
}
}
}
}
return this;
}
return NotificationCenter;