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

ts-models

v0.0.13

Published

An implementation of models at TypeScript 2.0.

Downloads

10

Readme

ts-models

An implementation of models at TypeScript 2.0.

Features

  1. Virtual models - lightweight clone of the target instance without a deep clone of the attributes of the original model. The models have own state.
  2. Support a Flux application architecture (state: running, pending values, etc).
  3. Full Angular 2.0.0 two-way binding compatible.
  4. Basic methods supported, such as commit, rollback, getFieldValue, setFieldValue, toJSON, etc.

Dependencies

Installation

First you need to install the npm module:

npm install ts-models --save

Use case #1

Some main methods are present in the model:

  1. toJSON - returns the unmodifiable snapshot of original attributes.
  2. toAttributes - returns the modifiable snapshot of attributes with the changes if they exist (for binding at the Angular2 templates).
  3. commit - it must be called after the synchronization of client-server.
  4. getChanges - returns the unmodifiable snapshot of the changes of the original attributes.
import {Model} from 'ts-models/index';

export interface IPerson {
    id:number;
    name:string;
    sex:string;
}

export class Person extends Model<IPerson> {

    constructor(protected attributes:IPerson) {
        super(attributes);
    }
}
import {Component} from '@angular/core';

import {Person, IPerson} from '../models';

@Component(...)
export class Home {

    private person:Person = new Person({
        id: 1,
        name: 'Tim',
        sex: 'men'
    });

    private _virtualPerson1:Person;
    private _virtualPerson2:Person;

    private _virtualPerson1Fields:IPerson;
    private _virtualPerson2Fields:IPerson;
    private _personAttributes:IPerson;

    get personAttributes():IPerson {
        return this._personAttributes = this._personAttributes || this.person.toAttributes();
    }

    get virtualPerson1():Person {
        return this._virtualPerson1 = this._virtualPerson1 || this.person.toVirtualModel<Person>();
    }

    get virtualPerson2():Person {
        return this._virtualPerson2 = this._virtualPerson2 || this.person.toVirtualModel<Person>();
    }

    get virtualPerson1Attributes():IPerson {
        return this._virtualPerson1Fields = this._virtualPerson1Fields || this.virtualPerson1.toAttributes();
    }

    get virtualPerson2Attributes():IPerson {
        return this._virtualPerson2Fields = this._virtualPerson2Fields || this.virtualPerson2.toAttributes();
    }

    constructor(public title:Title) {
    }

    modelToString(model:IPerson):string {
        return JSON.stringify(model);
    }

    submitChanges() {
        console.log('Person changes are:', this.modelToString(this.person.getChanges()));

        console.log('Virtual person 1 name is:', this.virtualPerson1.getFieldValue('name'));
        console.log('Virtual person 1 changes are:', this.modelToString(this.virtualPerson1.getChanges()));
        console.log('Virtual person 2 changes are:', this.modelToString(this.virtualPerson2.getChanges()));

        this.virtualPerson1.start();        // Open transaction
        this.virtualPerson1.commit();       // Commit transaction
        console.log('After commit, changes of virtual person 1 are: ', this.modelToString(this.virtualPerson1.getChanges()));

        this.person.start();                // Open transaction
        this.person.rollback();             // Rollback transaction
        console.log('After rollback, the name of person is: ', this.person.getFieldValue('name'));
    }
}
<form (ngSubmit)="submitChanges()" autocomplete="off">
  <br>
  <input type="text" [(ngModel)]="personAttributes.name" [ngModelOptions]="{standalone: true}"/>
  <input type="text" [(ngModel)]="virtualPerson1Attributes.name" [ngModelOptions]="{standalone: true}"/>
  <input type="text" [(ngModel)]="virtualPerson2Attributes.name" [ngModelOptions]="{standalone: true}"/><br>
  <br>
  Person: {{ modelToString(person.getChanges()) }}<br>
  Virtual person 1:{{ modelToString(virtualPerson1.getChanges()) }}<br>
  Virtual person 2:{{ modelToString(virtualPerson2.getChanges()) }}<br>

  <button>Submit changes</button>
</form>

Preview
Preview

Use case #2

You can also use the @ViewField and @ViewSourceModel annotations.

import {Component} from '@angular/core';

import {Model, ViewSourceModel, ViewField} from 'ts-models/index';

export interface IPerson {
    id:number;
    name:string;
    sex:string;
}

export class Person extends Model<IPerson> {

    constructor(protected attributes:IPerson) {
        super(attributes);
    }
}

@Component(...)
export class AboutComponent {

    private _person:Person = new Person({
        id: 1,
        name: 'Tim',
        sex: 'men'
    });

    @ViewField()
    private name:string;

    @ViewSourceModel()
    get viewPerson():Person {
        return this._person;
    }

    modelToString(model:IPerson):string {
        return JSON.stringify(model);
    }

    submitChanges() {
        console.log('Person changes are:', this.modelToString(this._person.getChanges()));

        this._person.start();                // Open transaction
        this._person.rollback();             // Rollback transaction
        console.log('After rollback, the name of person is: ', this._person.getFieldValue('name'));
    }
}
<form (ngSubmit)="submitChanges()" autocomplete="off">
  <br>
  <input type="text" [(ngModel)]="name" [ngModelOptions]="{standalone: true}"/><br>
  <br>
  Person: {{ modelToString(viewPerson.getChanges()) }}<br>
  <button>Submit changes</button>
</form>

Preview

Publish

npm run deploy

License

Licensed under MIT.