Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
img
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

observer_pattern

Follow JavaScript design pattern(observer pattern) and implementing this pattern.

Alt text

DevNote

Intro

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

Usage

publish

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);

unPublish

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);

fire

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);

About

Follow JavaScript design pattern(observer pattern) and implementing this pattern.

Resources

Releases

No releases published

Packages

No packages published