https://github.com/bakerface/take-action
A fast, unopinionated, minimalist action framework for JavaScript.
https://github.com/bakerface/take-action
Last synced: 10 months ago
JSON representation
A fast, unopinionated, minimalist action framework for JavaScript.
- Host: GitHub
- URL: https://github.com/bakerface/take-action
- Owner: bakerface
- License: mit
- Created: 2016-09-09T19:14:27.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-10-12T17:24:06.000Z (over 9 years ago)
- Last Synced: 2025-06-18T03:09:33.897Z (11 months ago)
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# take-action
[](https://travis-ci.org/bakerface/take-action)
[](https://npmjs.com/package/take-action)
[](https://npmjs.com/package/take-action)
[](https://codeclimate.com/github/bakerface/take-action)
[](https://codeclimate.com/github/bakerface/take-action)
### Action.create(action)
Creates an action with the given definition. Returns a function that accepts `jacks` and `props` as arguments, respectively. This function will validate the jacks using `jackTypes` and the props using `propTypes` before performing the action. Additionally, defaults can be supplied by providing `getDefaultJacks` and `getDefaultProps` functions.
``` javascript
var Action = require('take-action');
var createUser = Action.create({
name: 'createUser',
description: 'Creates a new user account',
jackTypes: {
users: Action.Types.shape({
add: Action.Types.func.isRequired
}).isRequired
},
propTypes: {
id: Action.Types.string.isRequired,
created: Action.Types.date.isRequired,
email: Action.Types.email.isRequired,
isAdmin: Action.Types.bool
}
getDefaultProps: function () {
return {
id: uuid.v4(),
created: Date.now()
};
},
perform: function (jacks, props) {
return jacks.users.add(props);
}
});
// a jack is an interface for external objects
// a plug is an implementation of a jack
var jacks = {
users: new RedisUserPlug()
};
var props = {
email: 'john@doe.com'
};
createUser(jacks, props);
```
### Action.bindActionsToJacks(actions, jacks)
Returns a new set of actions, each bound to `jacks`, accepting `props` as the only argument.
``` javascript
var Action = require('take-action');
var actions = {
hello: function (jacks, props) {
jacks.lang.hello(props.name);
},
goodbye: function (jacks, props) {
jacks.lang.goodbye(props.name);
},
};
var jacks = {
lang: {
hello: function (name) {
console.log('Hello, ' + name);
},
goodbye: function (name) {
console.log('Goodbye, ' + name);
}
}
};
var Greetings = Action.bindActionsToJacks(actions, jacks);
var user = {
name: 'John'
};
Greetings.hello(user); // Hello, John
Greetings.goodbye(user); // Goodbye, John
```