changeable

Listen for object changes

Usage no npm install needed!

<script type="module">
  import changeable from 'https://cdn.skypack.dev/changeable';
</script>

README

changeable

Listen for object changes

Example

var changeable = require('changeable');
var obj = changeable({ foo: 'bar' });

obj.on('change', function (prop, value, prev) {
    console.log('something changed');
});

obj.on('change foo', function (value, prev) {
    console.log('obj.foo changed');
});

obj.on('change foo.bar', function (value, prev) {
    console.log('obj.foo.bar changed');
});

obj.foo = { bar: 'baz' };
obj.foo.bar = 'qux';

Install

npm install changeable

API

.on(event, fn)

Register event handler function. change and change <path> events are emitted. See https://github.com/component/emitter for more about the event emitter API.

.silent(fn)

Execute the given function while the object's change events are silenced.

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

model.on('change', function () {
    console.log('will not get here');
});

model.silent(function () {
    model.foo = 'baz';
});

License

Copyright (C) 2014 Scott Nelson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.