Permalink
Browse files

fixed; reset modified paths and atomics after saved

  • Loading branch information...
1 parent ec5c757 commit f697135392ba189c1cd4bf02de1ad2a0279f1ea5 @aheckmann aheckmann committed Aug 30, 2011
Showing with 28 additions and 16 deletions.
  1. +26 −14 lib/model.js
  2. +1 −1 lib/query.js
  3. +1 −1 lib/types/document.js
View
@@ -239,6 +239,7 @@ function makeSaveHandler(promise, self) {
return function (err) {
if (err) return promise.error(err);
self.isNew = false;
+ self.reset();
self.emit('save', self);
promise.complete(self);
promise = null;
@@ -268,16 +269,7 @@ Model.prototype.save = function (fn) {
// send entire doc
this.collection.insert(this.toObject({ depopulate: 1 }), options, complete);
- // clear atomics
- this._dirty().forEach(function (dirt) {
- var type = dirt.value;
- if (type && type._path && type.doAtomics) {
- type._atomics = {};
- }
- });
-
} else {
- // optimization, _delta() clears atomics too
var delta = this._delta();
if (delta) {
@@ -287,11 +279,6 @@ Model.prototype.save = function (fn) {
}
}
- // Clear 'modify'('dirty') cache
- this._activePaths.clear('modify');
- this.schema.requiredPaths.forEach(function (path) {
- self._activePaths.require(path);
- });
};
/**
@@ -398,6 +385,31 @@ Model.prototype._delta = function () {
}, {});
}
+/**
+ * Resets the atomics and modified states of this document.
+ *
+ * @return {this}
+ */
+
+Model.prototype.reset = function reset () {
+ // clear atomics
+ this._dirty().forEach(function (dirt) {
+ var type = dirt.value;
+ if (type && type._path && type.doAtomics) {
+ type._atomics = {};
+ }
+ });
+
+ // Clear 'modify'('dirty') cache
+ this._activePaths.clear('modify');
+ var self = this;
+ this.schema.requiredPaths.forEach(function (path) {
+ self._activePaths.require(path);
+ });
+
+ return this;
+}
+
/**
* Remove the document
*
View
@@ -959,7 +959,7 @@ Query.prototype.update = function (doc, callback) {
Query.prototype._castUpdate = function (obj) {
var schema = this.model.schema
- , doc = new this.model // applies our defaults
+ , doc = new this.model
, paths = Object.keys(obj)
, i = paths.length
, subpaths
View
@@ -116,7 +116,7 @@ EmbeddedDocument.prototype.inspect = function () {
};
/**
- * Module exxports.
+ * Module exports.
*/
module.exports = EmbeddedDocument;

0 comments on commit f697135

Please sign in to comment.