action.js
A messaging system for javascript. This library also supports Promise.
How to use
Install
If use it in nodejs, install it use this command: npm install action.js
If use it in browser, intstall it use this command: bower install action.js
Create new instance:
- Nodejs:
var Action = require('./action.js'), scope = this;
var action = new Action(scope);- Brower:
var scope = this, action = new Action(scope);Or
require('action', function(Action){
var scope = this, action = new Action(scope);
});"scope" is an optional parameter which is used to change action listeners execute context. This libray is supports amd libraries such as requirejs.
Add Listener:
You can add listeners to an action instance's "before" or "after" field, "before" means these listeners will be executed before action code run. "after" means these listeners will be executed after action code run. You can add anonymous listener without depends.
action.after.add(function() {
return $.ajax('test.html');
});Or you can add anonymous listener with depends. the example below means only this listener will be executed only after a listener called "action1" executed. You also can get "action1" listener's result by parameter "action1Result".
action.after.add(['listener1'], function(listenerResults, actionResult) {
return $.ajax('test.html');
});Or you can add a named listener with depends.
action.after.add('listener2', ['listener1'], function(listenerResults, actionResult) {
console.log(listenerResults.listener1);
return $.ajax('test.html');
});Execute action:
action.take(function(){
});You can put any code you want to run in this function. this function can return a Promise object or anything else. If a Promise object is returned, the after listeners will run only after this Promise object returned.