1.4.8 • Published 10 months ago

@cognibox/cbx-client-model v1.4.8

Weekly downloads
34
License
MIT
Repository
-
Last release
10 months ago

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
1.4.8

10 months ago

5.0.3

1 year ago

1.4.7

1 year ago

5.0.2

1 year ago

1.4.6

1 year ago

5.0.1

1 year ago

1.4.5

1 year ago

5.0.0

4 years ago

4.12.5

4 years ago

4.12.2

5 years ago

4.12.0

5 years ago

4.12.1

5 years ago

4.11.1

5 years ago

4.11.0

5 years ago

4.10.0

5 years ago

4.9.3

6 years ago

4.9.2

6 years ago

4.9.1

6 years ago

4.9.0

6 years ago

4.8.4

6 years ago

4.8.3

6 years ago

4.8.2

6 years ago

4.8.1

6 years ago

4.8.0

6 years ago

4.0.7

6 years ago

4.0.6

6 years ago

4.0.5

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.4.4

6 years ago

1.4.3

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.3.0

6 years ago