flitbit / diff

Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
MIT License
3k stars 214 forks source link

Allow Custom Accessor and Mutator Functions #54

Open 0xcaff opened 9 years ago

0xcaff commented 9 years ago

I was wondering what the consensus was of adding an interface to provide the ability to use custom getters and setters? Possibly as an argument to applyChange or revertChange. This would be extremely useful in MVC frameworks like EmberJS which insist on using the provided accessors.

flitbit commented 8 years ago

Act like I don't know anything about EmberJS. What would it look like?

0xcaff commented 8 years ago

EmberJS uses Ember.Object objects instead of POJOs (Plain Old JavaScript Objects). Mutating or accessing an Ember Object is done by using Ember.Object.set(keyName, value) or Ember.Object.get(keyName). This is done to make observers work and allow unknown properties to be resolved dynamically. This model is used not only in Ember but in Backbone, Knockout and other frameworks which seek to maximize compatibility.

The interface would look something like this.

observableDiff(lhs, rhs, function (d) {
    applyChange(lhs, rhs, d, function(record) {
      lhs.set(record.path, record.rhs);
    });
});