A basic JavaScript model.
Server-side (Node.js):
$ npm install shannonmoeller/model.js
Client-side (component(1)):
$ component install shannonmoeller/model.js
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' });
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 }
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 }
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 }
$ npm test
MIT