Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement change tracking for models. #3

Open
kevinswiber opened this issue Oct 11, 2013 · 0 comments
Open

Implement change tracking for models. #3

kevinswiber opened this issue Oct 11, 2013 · 0 comments

Comments

@kevinswiber
Copy link
Owner

Harmony proxies would be awesome here, but we can't use them yet.

Because the model-based approach requires mapping of properties to the backing data store, we can use that information to create our own proxy.

In the example below, createProxy would have to be implemented into the model instantiation logic.

Change tracking will be important when Calypso implements save functionality. For document-level databases, this won't apply so much, but for relational, row-based databases that allow field-level updates, this will be perfect.

var ConstructorMap = require('calypso/constructor_map');

var Book = function() {
  this.title = null;
  this.author = null;
};

var mapping = function(config) {
}

var map = new ConstructorMap();
map.of(Book)
  .map('title')
  .map('author');

var createProxy = function(model) {
  var config = model.__orm_model_config__;
  var fields = Object.keys(config.fieldMap);
  fields.forEach(function(field) {
    model.prototype['_' + field] = null;
    model.prototype.__orm_change_tracking__ = [];
    Object.defineProperty(model.prototype, field, {
      get: function() {
        return this['_' + field];
      },
      set: function(val) {
        this['_' + field] = val;
        var changeTracking = this.__orm_change_tracking__;
        if (changeTracking.indexOf(field) === -1) {
          changeTracking.push(field);
        }
      }
    });
  });
};

createProxy(Book);

var book = new Book();

book.title = 'Hello';
book.author = 'World';

console.log(book.__orm_change_tracking__);

book.__orm_change_tracking__ = [];

book.title = 'Yo';

console.log(book.__orm_change_tracking__);

// Output:
// [ 'title', 'author' ]
// [ 'title' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant