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.
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
.
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.
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.
import { Store, action } from 'megalith';
class App extends Store {
initialState = 0;
@action increment() {
return this.state + 1;
}
}
Mark an existing method as an action method.
target
(Store): The target store prototype to define the action method onto.name
(String): The action name. Must be the same as the method name.
(undefined)
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.
target
(Store): The target store prototype to define the action method onto.name
(String): The action name.fn
(Function): An action function. Must return a new state object.
(undefined)
import { Store, action } from 'megalith';
class App extends Store {
initialState = 0;
}
action.define(App.prototype, 'increment', function() {
return this.state + 1;
});