Created
February 23, 2017 07:47
-
-
Save KhalilZaidoun/0e48a7142cdcabd9435485207953a680 to your computer and use it in GitHub Desktop.
Reactive store helper In Angular2 [ From Angular-class]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Store } from '../store'; | |
@Injectable() | |
export class StoreHelper { | |
constructor(private store: Store) {} | |
update(prop, state) { | |
const currentState = this.store.getState(); | |
this.store.setState(Object.assign({}, currentState, { [prop]: state })); | |
} | |
add(prop, state) { | |
const currentState = this.store.getState(); | |
const collection = currentState[prop]; | |
this.store.setState(Object.assign({}, currentState, { [prop]: [state, ...collection] })); | |
} | |
findAndUpdate(prop, state) { | |
const currentState = this.store.getState(); | |
const collection = currentState[prop]; | |
this.store.setState(Object.assign({}, currentState, {[prop]: collection.map(item => { | |
if (item.id !== state.id) { | |
return item; | |
} | |
return Object.assign({}, item, state) | |
})})) | |
} | |
findAndDelete(prop, id) { | |
const currentState = this.store.getState(); | |
const collection = currentState[prop]; | |
this.store.setState(Object.assign({}, currentState, {[prop]: collection.filter(item => item.id !== id)})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment