diff --git a/src/app/components/artists/artists.component.html b/src/app/components/artists/artists.component.html index 788087f..33d76a1 100644 --- a/src/app/components/artists/artists.component.html +++ b/src/app/components/artists/artists.component.html @@ -1,7 +1,6 @@

Artists

+ diff --git a/src/app/components/artists/artists.component.ts b/src/app/components/artists/artists.component.ts index dbeb22c..047e1f1 100644 --- a/src/app/components/artists/artists.component.ts +++ b/src/app/components/artists/artists.component.ts @@ -11,4 +11,10 @@ export class ArtistsComponent { public artists$ = this.artistsService.getAllModels(); constructor(private readonly artistsService: ArtistsService) {} + + public updateNames() { + this.artistsService.findAll().forEach((artist, index) => { + artist.name += index; + }); + } } diff --git a/src/app/models/artist.ts b/src/app/models/artist.ts index b82c40b..b78234d 100644 --- a/src/app/models/artist.ts +++ b/src/app/models/artist.ts @@ -3,7 +3,7 @@ import { BaseModel } from './base-model'; export class Artist extends BaseModel { public static endpoint = 'artists'; - public static type = 'project'; + public static type = 'artist'; @Attribute() public name!: string; diff --git a/src/app/models/base-model.ts b/src/app/models/base-model.ts index 78133ec..7db77b3 100644 --- a/src/app/models/base-model.ts +++ b/src/app/models/base-model.ts @@ -1,7 +1,37 @@ -import { IType, Model } from '@datx/core'; import { jsonapiAngular } from '@datx/jsonapi-angular'; +import { IRawModel, PatchType, PureCollection, IType, Model } from '@datx/core'; +import { getMeta } from '@datx/utils'; +import { BehaviorSubject, Observable } from 'rxjs'; export class BaseModel extends jsonapiAngular(Model) { + private _observables: Record> = {}; + + public $: { [key in 'id' | keyof Omit]: Observable } = {} as any; + + constructor(rawData?: IRawModel, collection?: PureCollection) { + super(rawData, collection); + + const fields = [...Object.keys(getMeta(this, 'fields', {})), 'id']; + fields.forEach((key) => { + this._observables[key] = new BehaviorSubject((this as any)[key]); + (this.$ as any)[key] = this._observables[key].asObservable(); + }); + + this.onPatch((patch) => { + if (patch.patchType === PatchType.UPDATE) { + const newValue = patch.newValue!; + Object.keys(newValue).forEach((key) => { + if (key in this._observables) { + this._observables[key].next((newValue as any)[key]); + } else { + this._observables[key] = new BehaviorSubject((newValue as any)[key]); + (this.$ as any)[key] = this._observables[key].asObservable(); + } + }); + } + }); + } + public get id(): IType { return this.meta.id; }