Created
March 3, 2019 20:59
-
-
Save vcpablo/57b8133bae072e1d9fb179ac341c613a to your computer and use it in GitHub Desktop.
Ionic + Akita state management issue
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
// Required imports | |
export class ContactsTab1Page implements OnInit { | |
contacts$: Observable<Contact[]>; | |
} | |
// Constructor | |
// Executed each time the tab is activated | |
ionViewWillEnter(){ | |
this.contactsService.get().subscribe(); | |
this.contacts$ = this.contactsQuery.selectAll(); | |
} | |
toggleStarred(contact: Contact) { | |
let data = Object.assign({}, contact); | |
data.starred = !data.starred; | |
this.contactsService.updateContact(data); | |
} |
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
// Required imports | |
export class ContactsTab2Page implements OnInit { | |
contacts$: Observable<Contact[]>; | |
} | |
// Constructor | |
// Executed each time the tab is activated | |
ionViewWillEnter(){ | |
this.contactsService.get().subscribe(); | |
this.contacts$ = this.contactsQuery.selectAll(); | |
} | |
toggleStarred(contact: Contact) { | |
let data = Object.assign({}, contact); | |
data.starred = !data.starred; | |
this.contactsService.updateContact(data); | |
} |
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 { QueryEntity, ID } from '@datorama/akita'; | |
import { Injectable } from '@angular/core'; | |
import { ContactsStore, State } from './contacts.store'; | |
import { Contact } from './contact.model'; | |
@Injectable() | |
export class ContactsQuery extends QueryEntity<State, Contact> { | |
constructor(protected store: ContactsStore) { | |
super(store); | |
} | |
} |
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
// Required imports | |
// Returns all contacts | |
get() { | |
const request = this.contactsDataService.getContacts().pipe( | |
tap(contacts => this.contactsStore.set(contacts)) | |
); | |
return this.contactsStore.isPristine ? request : noop(); | |
} | |
// Updates a contact | |
updateContact(contact: Contact) { | |
this.contactsStore.createOrReplace(contact.id, { ...contact }) | |
} |
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 { Contact } from './contact.model'; | |
import { EntityState, EntityStore, StoreConfig } from '@datorama/akita'; | |
import { Injectable } from '@angular/core'; | |
export interface State extends EntityState<Contact> {} | |
@Injectable() | |
@StoreConfig({ name: 'contacts', resettable: true }) | |
export class ContactsStore extends EntityStore<State, Contact> { | |
constructor() { | |
super(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment