Immer

Data Strategy

todos.actions.ts
import { Store, HasStore, InjectStore } from "ng-state";
import { TodoModel } from "./todo.model";

@InjectStore('todos')
export class TodosStateActions extends HasStore<List<any>> {
    addTodo(item: TodoModel) {
        this.store.update(state => {
            state.push(item);
        })
    }

    deleteTodo(index: number) {
        this.store.update(state => {
             if (index > -1) {
                state.splice(index, 1);
            }
        });
    }

    get todos() {
        return this.store.map((state) => {
            return state;
        });
    } // async way

    /// OR

    get todos() {
      return this.state;
    } // non async way
}

Last updated