forked fromsimplifiedcourses/observable-state
- Notifications
You must be signed in to change notification settings - Fork0
Custom written implementation for State Management in Angular
License
NotificationsYou must be signed in to change notification settings
fMoro1999/observable-state
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This repository explains a custom implementation about Observable State explained in these articles (newest to oldest):
- Observable state in Angular
- How to prepare for Angular signals
- Evolving from the SIP principle towards observable state
- Observable component state in Angular
- Observable state for ui components in Angular
- Reactive input state for Angular ViewModels
- Reactive ViewModels for Ui components in Angular
import{InputState}from'./input-state-decorator';import{ObservableState}from'./observable-state';typeMyUiComponentInputState={firstName:string;lastNam:string;age:number;}typeMyUiComponentState=MyUiComponentInputState&{showAge:number;}@Component({template:` <ng-container *ngIf="vm$|async as vm"> <p>First Name: {{vm.firstName}}</p> <p>Last Name: {{vm.lastName}}</p> <p *ngIf="vm.showAge">Age: {{vm.age}}</p> <button (click)="toggle()">Toggle Age</button> </ng-container> `})exportclassMyUiComponentextendsObservableState<MyUiComponentState>{ @InputState()publicreadonlyinputState$!:Observable<MyUiComponentInputState> @Input()publicfirstName=''; @Input()publiclastName=''; @Input()publicage=0;constructor(){super();this.initialize({firstName:'Brecht',lastName:'Billiet',age:35,showAge:false,},this.inputState$)}publicreadonlyvm$=this.state$.pipe(map(state)=>{...});publictoggle():void{this.patch({showAge:!this.snapshot.showAge})}}
In the following example we addedtime
to show the current time every second.
import{InputState}from'./input-state-decorator';import{ObservableState}from'./observable-state';typeMyUiComponentInputState={firstName:string;lastNam:string;age:number;}typeMyUiComponentState=MyUiComponentInputState&{showAge:number;time:number;}@Component({template:` <ng-container *ngIf="vm$|async as vm"> <p>First Name: {{vm.firstName}}</p> <p>Last Name: {{vm.lastName}}</p> <p *ngIf="vm.showAge">Age: {{vm.age}}</p> The time is {{vm.time|date:'hh:mm:ss'}} <button (click)="toggle()">Toggle Age</button> </ng-container> `})exportclassMyUiComponentextendsObservableState<MyUiComponentState>{ @InputState()publicreadonlyinputState$!:Observable<MyUiComponentInputState> @Input()publicfirstName=''; @Input()publiclastName=''; @Input()publicage=0;constructor(){super();this.initialize({firstName:'Brecht',lastName:'Billiet',age:35,showAge:false,time:newDate().getTime()},this.inputState$);this.connect({time:interval(1000).pipe(map(()=>newDate().getTime()))})}publicreadonlyvm$=this.state$;publictoggle():void{this.patch({showAge:!this.snapshot.showAge})}}
Run backend with
json-server backend/db.json
Run frontend with
npm start
About
Custom written implementation for State Management in Angular
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Languages
- TypeScript75.4%
- SCSS12.8%
- HTML11.8%