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.
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: '[email protected]'
};
createUser(jacks, props);
Returns a new set of actions, each bound to jacks
, accepting props
as the only argument.
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