All Projects → tomastrajan → Ngx Model

tomastrajan / Ngx Model

Licence: mit
Angular Model. Simple state management with minimalistic API, one way data flow, multiple model support and immutable data exposed as RxJS Observable.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ngx Model

mutable
State containers with dirty checking and more
Stars: ✭ 32 (-76.64%)
Mutual labels:  state-management, model, data-flow
Model
Angular Model - Simple state management with minimalist API, one way data flow, multiple model support and immutable data exposed as RxJS Observable.
Stars: ✭ 242 (+76.64%)
Mutual labels:  model, state-management
Westore
更好的小程序项目架构
Stars: ✭ 3,897 (+2744.53%)
Mutual labels:  state-management, model
realar
5 kB Advanced state manager for React
Stars: ✭ 41 (-70.07%)
Mutual labels:  model, data-flow
Datx
DatX is an opinionated JS/TS data store. It features support for simple property definition, references to other models and first-class TypeScript support.
Stars: ✭ 111 (-18.98%)
Mutual labels:  model, state-management
Codeigniter Model
CodeIgniter 3 Active Record (ORM) Standard Model with Laravel Eloquent & Yii2 AR like
Stars: ✭ 124 (-9.49%)
Mutual labels:  model
Readme Model
💾 A beautiful readme model for you to put in your projects.
Stars: ✭ 131 (-4.38%)
Mutual labels:  model
Dffml
The easiest way to use Machine Learning. Mix and match underlying ML libraries and data set sources. Generate new datasets or modify existing ones with ease.
Stars: ✭ 123 (-10.22%)
Mutual labels:  data-flow
Covasim
COVID-19 Agent-based Simulator (Covasim): a model for exploring coronavirus dynamics and interventions
Stars: ✭ 122 (-10.95%)
Mutual labels:  model
Freactal
Clean and robust state management for React and React-like libs.
Stars: ✭ 1,676 (+1123.36%)
Mutual labels:  state-management
Eager Load Pivot Relations
Eager load pivot relations for Laravel Eloquent's BelongsToMany relation.
Stars: ✭ 134 (-2.19%)
Mutual labels:  model
Voc
A physical model of the human vocal tract using literate programming, based on Pink Trombone.
Stars: ✭ 129 (-5.84%)
Mutual labels:  model
Prowide Core
Model and parsers for all SWIFT MT (FIN) messages
Stars: ✭ 125 (-8.76%)
Mutual labels:  model
Multiple Counters Flutter
Flutter State Management [ setState ❖ StreamBuilder ❖ scoped_model ❖ redux ]
Stars: ✭ 131 (-4.38%)
Mutual labels:  state-management
Awesome Python Models
A curated list of awesome Python libraries, which implement models, schemas, serializers/deserializers, ODM's/ORM's, Active Records or similar patterns.
Stars: ✭ 124 (-9.49%)
Mutual labels:  model
React In Patterns
A free book that talks about design patterns/techniques used while developing with React.
Stars: ✭ 10,948 (+7891.24%)
Mutual labels:  data-flow
Desktop Profiles
An innovative desktop manager for macOS
Stars: ✭ 122 (-10.95%)
Mutual labels:  state-management
Ayanami
🍭 A better way to react with state
Stars: ✭ 129 (-5.84%)
Mutual labels:  state-management
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (-2.92%)
Mutual labels:  state-management
Stateshot
💾 Non-aggressive history state management with structure sharing.
Stars: ✭ 128 (-6.57%)
Mutual labels:  state-management

⚠️ IMPORTANT

THIS REPO / PACKAGE HAS BEEN DEPRECATED

Please use new @angular-extensions/modelpackage / repo which is a combination of both the model library and related schematics which renders this package uselsess. On the other hand, feel free to keep using ngx-model if it suits your needs, it will not be deleted, but there will be no further development. Please, have a look into migration section in the new documentation.

The Angular Model - ngx-model

by @tomastrajan

npm npm npm Build Status Twitter Follow

Simple state management with minimalistic API, one way data flow, multiple model support and immutable data exposed as RxJS Observable.

Documentation

ngx-model dataflow diagram

Getting started

  1. Install ngx-model

    npm install --save ngx-model
    

    or

    yarn add ngx-model
    
  2. Import and use NgxModelModule in you AppModule (or CoreModule)

    import { NgxModelModule } from 'ngx-model';
        
    @NgModule({
      imports: [
        NgxModelModule
      ]
    })
    export class CoreModule {}
    
    
  3. Import and use Model and ModelFactory in your own services.

    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs';
    import { ModelFactory, Model } from 'ngx-model';
        
    @Injectable()
    export class TodosService {
            
      private model: Model<Todo[]>;
      
      todos$: Observable<Todo[]>;
            
      constructor(private modelFactory: ModelFactory<Todo[]>) {
        this.model = this.modelFactory.create([]); // create model and pass initial data
        this.todos$ = this.model.data$; // expose model data as named public property
      }
        
      toggleTodo(id: string) {
        // retrieve raw model data
        const todos = this.model.get();
            
        // mutate model data
        todos.forEach(t => {
          if (t.id === id) {
            t.done = !t.done;
          }
        });
            
        // set new model data (after mutation)
        this.model.set(todos);
      }
        
    }
    
  4. Use service in your component. Import and inject service into components constructor. Subscribe to services data in template todosService.todos$ | async or explicitly this.todosService.todos$.subscribe(todos => { /* ... */ })

    import { Component, OnInit, OnDestroy } from '@angular/core';
    import { Subject } from 'rxjs';
    
    import { TodosService, Todo } from './todos.service';
    
    @Component({
      selector: 'ngx-model-todos',
      templateUrl: `
        /* ... */
        <h1>Todos ({{count}})</h1>
        <ul>
          <!-- template subscription to todos using async pipe -->
          <li *ngFor="let todo of todosService.todos$ | async" (click)="onTodoClick(todo)">
            {{todo.name}}
          </li>
        </ul>
      `,
    })
    export class TodosComponent implements OnInit, OnDestroy {
    
      private unsubscribe$: Subject<void> = new Subject<void>();
      
      count: number;
     
      constructor(public todosService: TodosService) {}
    
      ngOnInit() {
        // explicit subscription to todos to get count
        this.todosService.todos
          .pipe(
            takeUntil(this.unsubscribe$) // declarative unsubscription
          )
          .subscribe(todos => this.count = todos.length);
      }
      
      ngOnDestroy(): void {
        // for declarative unsubscription
        this.unsubscribe$.next();
        this.unsubscribe$.complete();
      }
    
      onTodoClick(todo: Todo) {
        this.todosService.toggleTodo(todo.id);
      }
    
    }
    
    

Available Model Factories

Models are created using model factory as shown in above example this.model = this.modelFactory.create([]);. Multiple model factories are provided out of the box to support different use cases:

  • create(initialData: T): Model<T> - create basic model which is immutable by default (JSON cloning)
  • createMutable(initialData: T): Model<T> - create model with no immutability guarantees (you have to make sure that model consumers don't mutate and corrupt model state) but much more performance because whole cloning step is skipped
  • createMutableWithSharedSubscription(initialData: T): Model<T> - gain even more performance by skipping both immutability and sharing subscription between all consumers (eg situation in which many components are subscribed to single model)
  • createWithCustomClone(initialData: T, clone: (data: T) => T) - create immutable model by passing your custom clone function (JSON cloning doesn't support properties containing function or regex so custom cloning functionality might be needed)

Relationship to Angular Model Pattern

This is a library version of Angular Model Pattern. All the original examples and documentation are still valid. The only difference is that you can install ngx-model from npm instead of having to copy model pattern implementation to your project manually.

Check out the Blog Post and Advanced Usage Patterns for more how-tos and examples.

Getting started with Schematics

  1. make sure you're using this in project generated with Angular CLI.
  2. install dependency with npm i -D @angular-extensions/schematics
  3. generate model services with ng g @angular-extensions/schematics:model --name path/my-model
  4. or with ng g @angular-extensions/schematics:model --name path/my-model-collection --items form model of collection of items
  5. add your own model service methods and tests

Generating model using schematics

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].