3.2.2 • Published 8 years ago

take-action v3.2.2

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

take-action

build npm downloads climate coverage

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.

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.

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
3.2.2

8 years ago

3.2.1

8 years ago

3.2.0

8 years ago

3.1.0

8 years ago

3.0.0

8 years ago

2.0.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago