Commit 4003ab14 authored by Sebastien Robin's avatar Sebastien Robin

add skeleton of a more object oriented javascript (By Nicolas Petton)

parent 3f0be792
var announcement = function(spec) {
var spec = spec || {};
var that = {};
var callbacks = [];
that.name = spec.name;
that.add = function(callback) {
callbacks.push(callback);
};
that.remove = function(callback) {
//TODO;
};
that.register = function() {
announcer.register(that);
};
that.unregister = function() {
announcer.unregister(that);
};
that.trigger = function(args) {
for(var i=0; i<callbacks.length; i++) {
callbacks[i].apply(null, args);
};
};
return that;
};
var announcer = (function() {
var that = {};
var announcements = {};
that.register(name) {
if(!announcements[name]) {
announcements[name] = announcement();
}
};
that.unregister = function(name) {
//TODO
};
that.at = function(name) {
return announcements[name];
};
that.on = function(name, callback) {
that.register(name);
that.at(name).add(callback);
};
that.trigger(name, args) {
that.at(name).trigger(args);
}
return that;
}());
var command = function(spec) {
var spec = spec || {};
var that = {};
var id = idHandler.nextId();
var date = new Date();
that.document = spec.document;
that.getDate = function() {
return date;
};
that.getId = function() {
return id;
};
that.label = function() {
return 'command';
};
/*
* Specialized commands that override this should also call `super`
*/
that.validate = function(handler) {
that.validateState();
};
/*
* Delegate actual excecution the the handler object
*/
that.execute = function(handler) {
handler.execute(that);
};
that.executeOn = function(storage) {
};
/*
* Do not override.
* Override `validate()` instead
*/
that.validateState = function() {
if(!that.document) {
throw invalidCommandState(that);
};
};
return that;
};
var removeCommand = function(spec) {
var spec = spec || {};
var that = command(spec);
that.label = function() {
return 'removeCommand';
};
that.executeOn = function(storage) {
storage.executeRemove(that);
};
return that;
};
var saveCommand = function(spec) {
var spec = spec || {};
var that = command(spec);
that.label = function() {
return 'saveCommand';
};
that.executeOn = function(storage) {
storage.executeSave(that);
};
return that;
};
var document = function(spec) {
var spec = spec || {};
var that = {};
var name = spec.name;
var content = spec.content;
that.getName = function() {
return name;
};
that.getContent = function() {
return content;
};
}
var jioException = function() {
return {};
};
var invalidCommandState = function(command) {
var that = jioException();
that.command = command;
return that;
};
var idHandler = (function() {
var that = {};
var id = 0;
that.nextId = function() {
id = id + 1;
return id;
};
return that;
}());
var job = function(spec) {
var spec = spec || {};
var that = {};
var command = spec.command;
that.getCommand = function() {
return command;
};
that.serialized = function() {
return command.serialized();
};
return that;
};
var jobManager = (function(spec) {
var spec = spec || {};
var that = {};
var jobs = [];
}());
var storage = function(spec) {
var spec = spec || {};
var that = {};
that.execute = function(command) {
command.executeOn(that);
};
that.executeSave = function(command) {};
that.executeRemove = function(command) {};
return that;
}
var storageHandler = function(spec) {
var spec = spec || {};
var that = {};
var storages = spec.storages || [];
that.execute = function(command) {
that.validate(command);
that.doExecute(command);
};
that.doExecute = function(command) {
for(var i=0; i<storages.length; i++) {
storages[i].execute(command);
}
};
that.validate = function(command) {
command.validate(that);
};
return that;
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment