npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ng-clown/model

v0.0.4

Published

An angular library to manage state of angular projects. Using this models is very straightforward and very easy to understand and implement than using redux pattern and other state management libraries.

Downloads

11

Readme

Model

An angular library to manage state of angular projects. Using this models is very straightforward and very easy to understand and implement than using redux pattern and other state management libraries.

Installation

To install this package, just run the following command in the root directory of your angular project.

npm i @ng-clown/model

Create a model

To create a model, we can use create a class and decorate it with @Injectable and add extends BaseModel from this package.

Example.

import { Injectable } from "@angular/core";
import { BaseModel } from "@ng-clown/model";

@Injectable({ providedIn: 'root' })
export class User extends BaseModel {
    firstName: string = 'John';
    lastName: string = 'Doe';
}

Subscribe to changes

To subscribe to changes, we just need to inject the model to our component and subscribe to its observable property.

Example.

import { Component, OnInit } from '@angular/core';
import { User } from './user.model';

@Component({ ... })
export class AppComponent implements OnInit {

  constructor(private user: User) { }

  ngOnInit() {
    this.user.observable.subscribe(state => {
      console.log(state);
    });
  }

}

Emitting changes

Changing a value in the model's property will emit the changes and notify all the subscribers that there is a change in a model's property.

import { Component, OnInit } from '@angular/core';
import { User } from './user.model';

@Component({ ... })
export class AppComponent implements OnInit {

  constructor(private user: User) { }

  ngOnInit() {
    this.user.observable.subscribe(state => {
      console.log(state);
    });

    // Changing the value of a property will emit the changes to the subscribers
    this.user.firstName = 'Johnny';
  }

}

Note that changing multiple property will emit the changes multiple times. To change multiple property and emit the changes one time, we need to change the properties inside a method of the model. Please check the "Emitting via methods" section for more info.

Emitting via methods

To change multiple properties and broadcast the change only once, the properties needs to be updated inside a model property.

Example.

The model

import { Injectable } from "@angular/core";
import { BaseModel } from "@ng-clown/model";

@Injectable({ providedIn: 'root' })
export class User extends BaseModel {
    firstName: string = 'John';
    lastName: string = 'Doe';

    public setName(firstName: string, lastName: string) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

The component

import { Component, OnInit } from '@angular/core';
import { User } from './user.model';

@Component({ ... })
export class AppComponent implements OnInit {

  constructor(private user: User) { }

  ngOnInit() {
    this.user.observable.subscribe(state => {
      console.log(state);
    });

    // Changing multiple properties inside a model method will broadcast the changes only once.
    this.user.setName('Johnny', 'Smith');
  }

}

Unsubscribe all subscriptions

A model will always keeps track of the subscriptions and we can unsubscribe it all at once using unsubscribeAll method.

Example.

import { Component, OnInit, OnDestroy } from '@angular/core';
import { User } from './user.model';

@Component({ ... })
export class AppComponent implements OnInit, OnDestroy {

  constructor(private user: User) { }

  ngOnInit() {
    this.user.observable.subscribe(state => {
      console.log(state);
    });
    this.user.observable.subscribe(state => {
      console.log(state);
    });
  }

  ngOnDestroy() {
    this.user.unsubscribeAll();
  }

}

Unsubscribe a subscriptions

To unsubscribe a single subscription we can do it like how we usually unsubscribe an rxjs subscription.

import { Component, OnInit, OnDestroy } from '@angular/core';
import { User } from './user.model';

@Component({ ... })
export class AppComponent implements OnInit, OnDestroy {
  constructor(private user: User) { }

  private subscription: Subscription;

  ngOnInit() {
    this.subscription = this.user.observable.subscribe(state => {
      console.log(state);
    });
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }

}