Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Simple Typescript models

npm version Build Status codecov npm


npm install simple-ts-models

DataModel Usage

DataModels can be used to create simple objects that will trigger change events when any of the values change.

import {DataModel} from "simple-ts-models";

// Create a data model from a Javascript object
let m = new DataModel({
    foo: 1,
    bar: 'baz'

// Bind to changes once on foo field
m.once('change:foo', (value, previousValue) => {

}); = 5; // will call function above = 3; // will not call function above

// Bind to changes on bar field
m.bind('change:bar', (value, previousValue) => {

}); = 5; // will call the function above = 'foo'; // will also call the function above

// Bind to changes on all fields
m.bind('change', () => {

}); = 100; // will call the function above = 'bar'; // will also call the function above

m.getData(); // Will return {foo: 100, bar: 'bar'}

Model Usage

Models need to be extended and have fields defined in order to set data on them. You can specify field types in the @field decorator. You can easily create your own custom field types for validating your data and implementing your own data manipulation.

import {Model, fields} from "simple-ts-models";

class MyModel extends Model {
    id: number;

    foo: string;

    bar: any;

let m = new MyModel({
    id: 1 // Can set values from constructor

// Can set values from = 'string value'; // Will trigger 'change' & 'change:foo' events = [1,2,3];  // Will trigger 'change' & 'change:bar' events
m.getData(); // Returns {id: 1, foo: 'string value', bar: [1,2,3]}

Collection Usage

The collection class is an extension of Javascript's built in Array class that can have methods added to it to do operations on a collection of a specific model class.

class ExampleModel extends Model {
    id: number;

    is_default: boolean;

class ExampleCollection extends Collection<ExampleModel> {
    getDefault: () => ExampleModel = (): ExampleModel => {
        for(const i of this) {
                return i;

let m1 = new ExampleModel({id: 1, is_default: false}),
    m2 = new ExampleModel({id: 2, is_default: true}),
    m3 = new ExampleModel({id: 3, is_default: false}),
    c = new ExampleCollection(m1, m2);

// Can use array methods
c.length; // Returns 3

// Use method added created in ExampleCollection
c.getDefault() === m2; // true

// Use Collection method getData to get an array of objects containing the data in each model
c.getData(); // Returns [{id: 1, is_default: false},{id: 2, is_default: true},{id: 3, is_default: false}]
You can’t perform that action at this time.