@erikyuzwa/model

0.8.0 • Public • Published

model

Because "Entity" was taken.

This is a simple JavaScript Object wrapper. Turns a given list into object attributes, and can handle some basic callback execution upon attributes being changed.

Just have fun with it!

installation

npm install @erikyuzwa/model

using

// option (a) using require
const Model = require('model')

// option (b) using import
import Model from 'model'

// create a Model instance
// each instance generates a unique-ish id
// can initialize with an Object of attributes
const a = new Model({'name': '', 'address': '', 'city':''})

// or can initialize with an Array of attribute names
const a = new Model(['name', 'address', 'city'])

// set an attribute
a.name = 'Scott Pilgrim'

// get an attribute
a.name // "Scott Pilgrim"

// get the id
a.id

// get a list of the attributes
Object.keys(a) // ['id', 'name', 'address', 'city']

// register a callback for an attribute change
const handler = function (params) {
    // do something with the params
}

// register the callback on this Model instance
a.on('change', handler)

// when you update an attribute, the handler is fired!
a.name = 'James Bond'

Readme

Keywords

Package Sidebar

Install

npm i @erikyuzwa/model

Weekly Downloads

5

Version

0.8.0

License

MIT

Unpacked Size

19.4 kB

Total Files

7

Last publish

Collaborators

  • erikyuzwa