0.4.0 • Published 7 years ago

modl-js v0.4.0

Weekly downloads
8
License
Apache 2.0
Repository
-
Last release
7 years ago

Modl

A mutation base model management library, inspired by the Flux pattern.

Install

# yarn
yarn add modl-js
# npm
npm install modl-js

Usage

import Modl from 'modl-js';

Modl.use(Modl.middlewares.type)
    .use(Modl.middlewares.default);

const Person = Modl({
  fields: {
    fisrtName: {
      type: String
    },
    lastName: {
      type: String
    },
    birthDate: {
      type: Date,
      default() { 
        return new Date(); 
      }
    }
  },
  computed: {
    age(store) {
      const ageDif = new Date(Date.now() - store.birthDate.getTime());
      return Math.abs(ageDate.getUTCFullYear() - 1970);
    }
  }
});

const me = new Person({ 
  firstName: 'mod', 
  lastName: 'elo', 
  birthDate: new Date('2017-10-15T15:45:48Z') 
});

Mutating

import Modl from 'modl-js';

const Person = Modl({
  fields: {
    fisrtName: {
      type: String
    }
  },
  mutations: {
    CHANGE_NAME(store, name) {
      store.name = name;
    }
  }
});

const me = new Person({ 
  firstName: 'modl'
});
me.mutated(() => { console.info('changed'); });
me.commit('CHANGE_NAME', 'bob');

Actions

import Modl from 'modl-js';

const Person = Modl({
  fields: {
    fisrtName: {
      type: String
    }
  },
  mutations: {
    CHANGE_NAME(store, name) {
      store.name = name;
    }
  },
  actions: {
    setName(store, name) {
      return new Promise((resolve) => {
        setTimeout(() => {
          store.commit('CHANGE_NAME', name);
          resolve();
        }, 3000);
      });
    }
  }
});

const me = new Person({ 
  firstName: 'modl'
});
me.mutated(() => { console.info('changed'); });
me.setName('bob').then(() => {
  console.info('done');
});

Run tests

npm run test

Build

npm run build

Requirement

  • Object.assign
  • Array.concat
  • WeakMap
  • Set

Road map

  • Properties management
  • Actions and mutations management
  • Properties extansibility
  • Inheritance
  • Flat inheritance
  • [] Behavior management
  • [] Documentation
0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago