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

@cognibox/cbx-client-model

v1.4.8

Published

`npm i` ### Tests `npm run test` ### Linting `npm run lint` ### Build `npm run build` ## Usage ### Class creation A base model can be created to add global functionnalities: ```javascript import { Model } from 'cbx-client-model';

Downloads

749

Readme

Proxy-based collision-free pure-ES6 model layer for Cognibox

Commands

Installation

npm i

Tests

npm run test

Linting

npm run lint

Build

npm run build

Usage

Class creation

A base model can be created to add global functionnalities:

import { Model } from 'cbx-client-model';

class Base extends Model {
  static urlRoot() {
    return '/api';
  }
}

Each model can then extend the Base model:

import Base from './base';

class MyModel extends Base {
  ...
}

Models require a resource URL:

class MyModel extends Base {
  static urlResource() {
    return 'my-model';
  }
}

Attributes are specified by an instance method returning an object whose keys are attributes and whose values are instance of Attribute with keys

  • default
  • validations (required: default false)
  • autoValidate (default true)
import { Attribute, Model } from 'CbxClientModel';

class MyModel extends Model {
  buildFields() {
    return {
      id: new Attribute(),
      myName: new Attribute({
        value: 'Fred',
        autoValidate: false,
        validations: {
          required() { return this.value !== undefined; },
        },
      }),
    };
  }
}

Associations are specified by the same method as Attributes by passing Association instances (BelongsTo, HasOne, HasMany) instead of Attribute instances.

  • model (the class of the associated model)
import PhoneNumber from './phone-number';
import { HasOne, Model } from 'CbxClientModel';

class MyModel extends Model {
  buildFields() {
    return {
      phoneNumber: new HasOne({
        model: PhoneNumber,
        value: '',
      }),
    };
  }
}

Attributes

Attributes are accessed through the fields property and then through value

import MyModel from './my-model';

const myModel = new MyModel({ id: 1 });
myModel.fields.id.value; // 1

The changes and hasChanged properties track changed on attributes and on the model. Changed can be reset using the setPristine method

import MyModel from './my-model';

const myModel = new MyModel({ id: 1 });
myModel.fields.id.value; // 1
myModel.fields.id.hasChanged; // false
myModel.fields.id.value = 2;
myModel.fields.id.hasChanged;// true
myModel.hasChanged; // true
myModel.fields.id.changes; // { oldValue: 1, newValue: 2 }
myModel.changes; // { id: { oldValue: 1, newValue: 2 } }
myModel.fields.id.setPristine();
myModel.fields.id.hasChanged; // false

Model fetching

A single model can be fetched by instantiating it with an id

import MyModel from './my-model';

const myModel = new MyModel({ id: 1 });
myModel.fetch();

The fetch method also takes an argument transferred in the payload

myModel.fetch({ fields: ['name'] });

All models can be fetched using the fetchAll method

const allModels = MyModel.fetchAll();

All element of an association can also be fetched using fetch

const myModel = new MyModel({ id: 1 });
myModel.fetch();
myModel.fields.phoneNumber.fetch();

Model saving

Models can then be saved to the server using the save method. Calls only occur if the model has changed. The save method returns a promise which resolves when the save is done. Once the model is saved, changes are reset.

const myModel = new MyModel({ id: 1 });
myModel.fetch();
myModel.name = 'Fredi';
myModel.hasChanged; // true
myModel.save();
myModel.hasChanged; // false