Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

simple-model

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-model

Simple model for getting and setting values

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

simple-model

Simple model for getting and setting values


var SimpleModel = require('simple-model');

var x = new SimpleModel();

x.on('change', function (prop, newVal, oldVal) {
    console.log('change ' + prop + ' from ' + oldVal + ' to ' + newVal);
});

x.set('foo', 'bar');
// 'change foo from undefined to bar'
x.set('foo', 'baz');
// 'change foo from bar to baz'

x.get('foo');
// 'baz'

Installation

$ npm install simple-model

Dependencies

  • deepequal
  • attach-middleware
  • emitter-context

API

SimpleModel

Create a new instance of a model

model.set(prop, value)

Set the value of prop as value

model.get(prop)

Return the value of prop

model.setMiddleware(fn)

Attach middleware for setting values

refer to attach-middleware for documentation

// resume from previous example
x.setMiddleware(function (prop, val, next) {
    next(null, prop.replace(/ /g, '_'), 'val: ' + val);
});

x.set('foo bar', 'baz');

x.get('foo bar');
// undefined

x.get('foo_bar');
// 'val: baz'

model.getMiddleware(fn)

Attach middleware for getting values

refer to attach-middleware for documentation

// resume from previous example
x.getMiddleware(function (prop, val, next) {
    next(null, prop.replace(/ /g, '_'), 'getMiddleware: ' + val);
});

x.get('foo bar');
// 'getMiddleware: val: baz'

Events

'change' (prop, newVal, oldVal)

Listen for all changes on the model

'change PROP' (newVal, oldVal)

Listen for a specific prop change on the model

Limitations

  • Getting values must be synchronous. Let me know if an asynchronous model is needed

Keywords

model

FAQs

Package last updated on 31 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts