Skip to content

Latest commit

 

History

History
147 lines (95 loc) · 3.17 KB

action.md

File metadata and controls

147 lines (95 loc) · 3.17 KB

megalith.action

Marks Store methods as action methods.

Action methods are expected to return a compete copy of the current state, with any modifications applied to the copy. The store's state can not be updated directly.

After being marked as an action method, the store will fire events whenever they are called.

Objects

Decorators

Functions


Objects

The action object created when calling an action method. Typically this is auto-generated for you, but you can create your own and dispatch them with Store.dispatch.

Properties
  • type (String): The action name.
  • payload (Array): The arguments to pass to the action method. This should contain any action-specific data needed to accomplish the task.

Decorators

A decorator to mark store methods as action methods.

Since decorators are not part of the ES spec yet, you'll need a pre-processor plugin such as: babel-plugin-transform-decorators-legacy.

Example
import { Store, action } from 'megalith';

class App extends Store {
  initialState = 0;

  @action increment() {
    return this.state + 1;
  }
}

Functions

Mark an existing method as an action method.

Arguments
  1. target (Store): The target store prototype to define the action method onto.
  2. name (String): The action name. Must be the same as the method name.
Returns

(undefined)

Example
import { Store, action } from 'megalith';

class App extends Store {
  initialState = 0;

  increment() {
    return this.state + 1;
  }
}

action.define(App.prototype, 'increment');

Add an action method.

Arguments
  1. target (Store): The target store prototype to define the action method onto.
  2. name (String): The action name.
  3. fn (Function): An action function. Must return a new state object.
Returns

(undefined)

Example
import { Store, action } from 'megalith';

class App extends Store {
  initialState = 0;
}

action.define(App.prototype, 'increment', function() {
  return this.state + 1;
});



Documentation