ImmutaleJs
Data Strategy
Signal actions
import { Store, HasSignalStore, InjectStore } from "ng-state";
import { TodoModel } from "./todo.model";
@InjectStore('todos')
export class TodosStateActions extends HasSignalStore<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.state();
}
}
State actions
todos.actions.ts
import { List, fromJS } from 'immutable';
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(fromJS(item));
})
}
deleteTodo(index: number) {
this.store.update(state => {
state.delete(index);
}, false);
}
get todos() {
return this.store.map((state) => {
return state.toArray();
});
} // async way
/// OR
get todos() {
return this.state.toArray();
} // non async way
}
Last updated