JSFiddle - React, Tailwind, and code Playground
by eddiarnoldo
JavaScript
//Ejemplo de mediator
Mediator = function() {
var debug = function(msg) {
// console.log or air.trace as desired
console.log(msg);
};
var components = {};
var broadcast = function(event, args, source) {
if (!event) {
return;
}
args = args || [];
//debug(["Mediator received", event, args].join(' '));
for (var c in components) {
if (typeof components[c]["on" + event] == "function" &&
components[c]['state'] != 'stopped') {
try {
//debug("Mediator calling " + event + " on " + c);
source = source || components[c];
components[c]["on" + event].apply(source, args);
} catch (err) {
debug(["Mediator error.", event, args, source, err].join(' '));
}
}
}
};
var addComponent = function(name, component, replaceDuplicate) {
if (name in components) {
if (replaceDuplicate) {
removeComponent(name);
} else {
throw new Error('Mediator name conflict: ' + name);
}
}
components[name] = component;
components[name]['state']='stopped';
};
var removeComponent = function(name) {
if (name in components) {
delete components[name];
}
};
var getComponent = function(name) {
return components[name]; // undefined if component has not been added
};
var contains = function(name) {
return (name in components);
};
var startModule = function(name){
components[name]['state']='started';
};
var stopModule =...