Follow JavaScript design pattern(observer pattern) and implementing this pattern.
This is min function observer pattern implementing
It can handle cross module communication
hub
is a array tmp actions
publish
include a action and something want to do and publish to hub
provide subscribe
unpublish
can cancel published action or something want to do
fire
is function trigger action, it is like subscribe in observer pattern
OB.publish(action, callback);
action[string
]: action for fire, it is not unique
callback[function
]: Something to do
function redAlert() {
alert('RED ALERT');
}
B.publish('alarm', redAlert);
OB.unPublish(action, callback name);
action[string
]: cancel publish action
callback name[var
]: it is a option if want cancel something want to do but in many actions, can point function name to cancel it
OB.unPublish('alarm');
// or
function redAlert() {
alert('alert');
}
OB.unPublish('', redAlert);
// or
OB.unPublish('alarm', redAlert);
OB.fire(action, callback);
action[string
]: fire action(s) to do something
callback[function
]: Something to do, it can handle if publish is async behavior
OB.fire('alarm');
// or
function funcA() {
console.log('fin');
}
OB.fire('alarm', funcA);