Javascript object encapsulation that tracks changes

Posted by Raynos on Stack Overflow See other posts from Stack Overflow or by Raynos
Published on 2011-01-06T23:30:51Z Indexed on 2011/01/07 9:53 UTC
Read the original article Hit count: 141

Is it possible to create an object container where changes can be tracked

Said object is a complex nested object of data. (compliant with JSON).

The wrapper allows you to get the object, and save changes, without specifically stating what the changes are

Does there exist a design pattern for this kind of encapsulation

Deep cloning is not an option since I'm trying to write a wrapper like this to avoid doing just that.

The solution of serialization should only be considered if there are no other solutions.

An example of use would be

var foo = state.get();
// change state
state.update(); // or state.save();
client.tell(state.recentChange());

A jsfiddle snippet might help : http://jsfiddle.net/Raynos/kzKEp/

It seems like implementing an internal hash to keep track of changes is the best option.

[Edit]

To clarify this is actaully done on node.js on the server. The only thing that changes is that the solution can be specific to the V8 implementation.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about design-patterns