Permalink
Browse files

Add Document.prototype.getModifiedPaths

  • Loading branch information...
1 parent 0494d4e commit d2db570048fd3c7a4d4cd9a15200ee1bdd3d3d70 @ljharb ljharb committed with aheckmann Feb 2, 2012
Showing with 27 additions and 10 deletions.
  1. +27 −10 lib/document.js
View
@@ -568,6 +568,32 @@ Document.prototype.try = function (fn, scope) {
return res;
};
+/**
+ * Gets a list of paths that have been directly modified.
+ *
+ * e.g., if we set `documents.0.title` to 'newTitle'
+ * then we have directly modified `documents.0.title`
+ * but not directly modified `documents` or `documents.0`.
+ * Nonetheless, we still say `documents` and `documents.0`
+ * are modified. They just are not considered direct modified.
+ * The distinction is important because we need to distinguish
+ * between what has been directly modified and what hasn't so
+ * that we can determine the MINIMUM set of dirty data
+ * that we want to send to MongoDB on a Document save.
+ */
+
+Document.prototype.getModifiedPaths = function () {
+ var directModifiedPaths = Object.keys(this._activePaths.states.modify),
+ allPossibleChains = directModifiedPaths.reduce(function (list, path) {
+ var parts = path.split('.');
+ return list.concat(parts.reduce(function (chains, part, i) {
+ return chains.concat(parts.slice(0, i).concat(part).join('.'));
+ }, []));
+ }, []);
+
+ return allPossibleChains;
+};
+
/**
* Checks if a path or any full path containing path as part of
* its path chain has been directly modified.
@@ -586,16 +612,7 @@ Document.prototype.try = function (fn, scope) {
*/
Document.prototype.isModified = function (path) {
- var directModifiedPaths = Object.keys(this._activePaths.states.modify);
-
- var allPossibleChains = directModifiedPaths.reduce(function (list, path) {
- var parts = path.split('.');
- return list.concat(parts.reduce(function (chains, part, i) {
- return chains.concat(parts.slice(0, i).concat(part).join('.'));
- }, []));
- }, []);
-
- return !!~allPossibleChains.indexOf(path);
+ return !!~(this.getModifiedPaths().indexOf(path));
};
/**

0 comments on commit d2db570

Please sign in to comment.