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

Add patch method #3

Open
ericclemmons opened this issue May 13, 2014 · 0 comments
Open

Add patch method #3

ericclemmons opened this issue May 13, 2014 · 0 comments

Comments

@ericclemmons
Copy link
Owner

The patch method should operate like save, except it should pre-load and compute the optimal diff for saving:

var diff = function(before, after) {
  var patch = after;

  if (angular.isArray(after)) {
    patch = [];
  } else if (angular.isObject(after)) {
    patch = {};
  }

  if (angular.isObject(before) && angular.isDefined(after)) {
    // Compute difference for changed properties
    angular.forEach(before, function(left, key) {
      if (!angular.isDefined(after[key])) {
        return false;
      }

      var right = after[key];

      if (angular.isArray(after) || !angular.equals(left, right) || key === 'id') {
        patch[key] = diff(left, right);
      }
    });

    // Add new properties to patch
    angular.forEach(after, function(right, key) {
      if (!angular.isDefined(before[key])) {
        patch[key] = right;
      }
    });
  }

  return patch;
};

The save method should no longer pre-fetch a deep tree, and only patch should do that for calculation only, then defer to save for the actual saving.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant