0.0.11 • Published 10 years ago
object-change v0.0.11
object-change
Simple utility to be able to attach listeners to events that fired when an object property changes
Overview
Basically using this utility on any plain object will create a new object that is an instance of a dispatcher and that will fire events when the "observed" properties change.
Install
npm i --save object-changeInterface
objChange( obj:Object, properties:Array ):dispatcher
Parameters
- obj:Object: The object with the initial values for the proxy instance.
- properties:Array: The properties that will trigger the change event when its values change.
Returns An instance of a dispatcher that fire:
- a
changeevent whenever any observed property changes. - a
change:NAME_OF_PROPevent whenever the observed propertyNAME_OF_PRODchanges
Example
var obj = {
someProp: '1',
anotherProp: 2
};
var objChange = require('object-change');
var proxyObj = objChange(obj, ['someProp']);
proxyObj.on('change:someProp', function (e, args) {
console.log('change from: '+ args.oldValue + ' to: ' + args.newValue);
});
proxyObj.someProp = 'some other value';
// ==> output <==
// change from: 1 to: 'some other value';
proxyObj.anotherProp = 1// doesn't fire change because is not observed.Important notes and limitations
- The events fire synchronously. So far I haven't had any problems. Make sure you don't do expensive tasks inside the
changelistener - The dispatcher instance returned have the following methods:
on. To add a listener to an event in the dispatcherfire. To fire an event on the event dispatcheroff. To remove the listeners The events works similarly to the jQueryon/off.fireis similar tojQuery.fn.triggerHandler
- Be aware that the properties should not be named
on,fireoroff - It does not work on nested properties. This is by design, I just wanted to keep it simple.
- The base object is used as the prototype for the returned object. If you change a nested property, it will affect the original object I did it this way to avoid having to copy all the properties to the new object, but... I might consider change it in the future if it is proven to be a problem.