Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Latest commit

 

History

History
79 lines (48 loc) · 1.53 KB

README.md

File metadata and controls

79 lines (48 loc) · 1.53 KB

Model.js Build Status

A basic JavaScript model.

Installation

Server-side (Node.js):

$ npm install shannonmoeller/model.js

Client-side (component(1)):

$ component install shannonmoeller/model.js

API

Model(object)

Create a new model which wraps around object. Context is enforced. Calling new Model, Model.apply, and Model.call will not affect the value of this in the constructor.

var Model = require('model');
var foo = Model({ foo: 'bar' });

.get(key):Any
.get(array):Object

Gets one or more values.

var foo = Model({ a: 1, b: 2 });

foo.get('a');             // 1
foo.get('b');             // 2
foo.get('c');             // undefined
foo.get(['a', 'b', 'c']); // { a: 1, b: 2, c: undefined }

.set(key, value):this
.set(object):this

Sets one or more values.

var foo = Model();

foo.toJSON(); // { }

foo.set('a', 1);
foo.toJSON(); // { a: 1 }

foo.set('b', 2);
foo.toJSON(); // { a: 1, b: 2 }

foo.set({ b: 3, c: 4 });
foo.toJSON(); // { a: 1, b: 3, c: 4 }

.toJSON()

Returns the current state of the internal data as a plain object.

var foo = Model({ a: 1, b: 2, c: 3 });

foo.toJSON(); // { a: 1, b: 2, c: 3 }

Testing

$ npm test

browser support

License

MIT