-
Notifications
You must be signed in to change notification settings - Fork 6
Actions
karelsteinmetz edited this page May 19, 2016
·
2 revisions
- returns new instances of modified state and its sub states
- beware on array operations like push etc.
- use as much as possible specific cursors
- if you want to modify more sub states then you should create two actions with specified cursors. Then invoke actions synchronously. b.invalidate waits for both actions. If actions take a long time then intermediate state will be rendered between actions.
- function should not be passed through parameters into action
- never throw in actions!!!
- implementation:
export let removeTodo = f.createAction<s.ITodo[], number>(c.todos, (todos, id) => {
return [...todos.filter(t => t.id !== id)];
});
- invoking:
actions.removeTodo(t.id);
- implementation:
export let removeTodoId1 = bobflux.createAction(cursors.todos, (todos: states.ITodo[]): states.ITodo[] => {
return [...todos.filter(t => t.id !== 1)];
});
- invoking:
actions.removeStaticTodo();
- implementation:
export interface IChangeDoneStatusParams {
id: number;
isDone: boolean;
}
export let changeDoneStatus = f.createAction<s.ITodo[], IChangeDoneStatusParams>(c.todos, (todos, params) => {
return todos.map(t => {
if (t.id === params.id)
return f.shallowCopy(t, (nT) => {
nT.isDone = params.isDone;
return nT;
});
return t;
})
});
- invoking:
actions.changeDoneStatus({ id: t.id, isDone: value })
- implementation:
let testAction = af.createAction<tds.ITodo, tds.ITodoParams>(
{
create: (params) => {
return { key: `todos.${params.index}` };
}
},
(state, params) => { return params.todo }
);
- invoking:
testAction({ index: 1, todo: { done: false, name: 'New second todo' } });