Skip to content

Instantly share code, notes, and snippets.

@michal
Created February 28, 2012 13:03
Show Gist options
  • Save michal/1932435 to your computer and use it in GitHub Desktop.
Save michal/1932435 to your computer and use it in GitHub Desktop.
var Proxy = {
create: function(module, key) {
return {
// dodajemy obietnice
promise: function(event) {
var def = $.Deferred();
event.data.resolve = def.resolve;
module.triggerEvent(event);
return def.promise();
},
// dodane obietnice zostaly spelione, rozwiazujemy
resolve: function(promises, event) {
$.when.apply(null, promises).then(function() {
module.triggerEvent(event);
});
},
// wywolujemy event
notify: function(event) {
module.triggerEvent(event);
},
// rejestrujemy event
listen: function(event) {
module.registerEvents(event, key);
},
// usuwamy event
ignore: function(event) {
module.unbindEvents(event, key);
}
};
}
};
var Module = (function() {
var _modules = {};
return {
create: function(key, process) {
var temp;
temp = process.call(this, Proxy.create(this, key));
if (temp.init) {
_modules[key] = {
create: process,
instance: null
};
}
},
start: function(data) {
var module;
switch (typeof data) {
case 'string':
module = _modules[data];
if (module) {
module.instance = module.create(Proxy.create(this, data));
module.instance.init();
}
break;
case 'object':
var i = 0;
for ( ; module = data[i++]; ) {
this.start(module);
}
break;
}
},
stop : function (key) {
var data;
if (data = _modules[key] && data.instance) {
data.instance.destroy();
data.instance = null;
}
},
registerEvents : function (events, key) {
if (events) {
if (_modules[key]) {
_modules[key].events = events;
}
}
},
triggerEvent : function (event) {
var module;
for (module in _modules) {
if (_modules.hasOwnProperty(module)) {
module = _modules[module];
if (module.events && module.events[event.type]) {
// module.events[event.type].call(this, event.data);
module.events[event.type](event.data);
}
}
}
},
removeEvents : function (events, module) {
if (events && module && (module = _modules[module]) && module.events) {
delete module.events;
}
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment