Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #52 from wbrady/master

Adds connection and callback as params to onSave
  • Loading branch information...
commit 36dcc7f815ed4e4e8e80db1e8b8df9b1217ec411 2 parents 62b7c8b + acc903c
@joeferner authored
Showing with 57 additions and 40 deletions.
  1. +5 −2 README.md
  2. +2 −1  examples/webapp/models/blog.js
  3. +50 −37 lib/connection.js
View
7 README.md
@@ -582,7 +582,7 @@ __Example__
});
<a name="modelOnSave" />
-### Model.onSave(obj)
+### Model.onSave(obj, connection, callback)
If preset this function will be called when an update or save occures. You would typically create this method
in your model file.
@@ -590,11 +590,14 @@ in your model file.
__Arguments__
* obj - The object or partial object, in the case of [update](#modelUpdate), being saved.
+ * connection - The connection persist is currently using to do the save
+ * callback() - The callback to be called when the onSave is complete
__Example__
- Person.onSave = function(obj) {
+ Person.onSave = function(obj, connection, callback) {
obj.lastUpdated = new Date();
+ callback();
};
<a name="modelOnLoad" />
View
3  examples/webapp/models/blog.js
@@ -13,6 +13,7 @@ module.exports = Blog = persist.define("Blog", {
.hasOne(Category)
.hasMany(Keyword, { through: "blogs_keywords" });
-Blog.onSave = function(obj) {
+Blog.onSave = function(obj, connection, callback) {
obj.lastUpdated = new Date();
+ callback();
}
View
87 lib/connection.js
@@ -81,59 +81,72 @@ var Connection = Class.extend({
}
}
- function doSave() {
+ function doOnSave(callback) {
if (obj._getModel().onSave) {
- obj._getModel().onSave(obj);
+ return obj._getModel().onSave(obj, self, callback);
}
- obj._getModel().emit("beforeSave", obj);
- if (isNew) {
- obj._getModel().emit("beforeCreate", obj);
- } else {
- obj._getModel().emit("beforeUpdate", obj);
- }
+ callback();
+ }
- var sqlAndValues;
- if (isNew) {
- sqlAndValues = self.driver.getInsertSql(obj);
- } else {
- sqlAndValues = self.driver.getUpdateSql(obj);
- }
- self._runSql(sqlAndValues.sql, sqlAndValues.values, function(err, data) {
- if (err) {
- callback(err);
- return;
+ function doSave() {
+ doOnSave(function() {
+ obj._getModel().emit("beforeSave", obj);
+
+ if (isNew) {
+ obj._getModel().emit("beforeCreate", obj);
+ } else {
+ obj._getModel().emit("beforeUpdate", obj);
}
- if (isNew && data.lastId) {
- var idPropName = obj._getModel().getIdPropertyName();
- obj[idPropName] = data.lastId;
+
+ var sqlAndValues;
+ if (isNew) {
+ sqlAndValues = self.driver.getInsertSql(obj);
+ } else {
+ sqlAndValues = self.driver.getUpdateSql(obj);
}
- obj._getConnection = function() { return self; }; // hide from JSON.stringify
- obj._isPersisted = function() { return true; };
- self.saveAssociations(obj, function(err, obj) {
- if (isNew) {
- obj._getModel().emit("afterCreate", obj);
- obj._getModel().emit("afterSave", obj);
- } else {
- obj._getModel().emit("afterUpdate", obj);
- obj._getModel().emit("afterSave", obj);
+ self._runSql(sqlAndValues.sql, sqlAndValues.values, function(err, data) {
+ if (err) {
+ callback(err);
+ return;
}
-
- callback.apply(self, arguments);
+ if (isNew && data.lastId) {
+ var idPropName = obj._getModel().getIdPropertyName();
+ obj[idPropName] = data.lastId;
+ }
+ obj._getConnection = function() { return self; }; // hide from JSON.stringify
+ obj._isPersisted = function() { return true; };
+ self.saveAssociations(obj, function(err, obj) {
+ if (isNew) {
+ obj._getModel().emit("afterCreate", obj);
+ obj._getModel().emit("afterSave", obj);
+ } else {
+ obj._getModel().emit("afterUpdate", obj);
+ obj._getModel().emit("afterSave", obj);
+ }
+
+ callback.apply(self, arguments);
+ });
});
});
}
},
updatePartial: function(model, id, data, callback) {
- var self = this;
+ function doOnSave(callback) {
+ if (model.onSave) {
+ return model.onSave(data, self, callback);
+ }
- if (model.onSave) {
- model.onSave(data);
+ callback();
}
- var sqlAndValues = this.driver.getUpdatePartialSql(model, id, data);
- this._runSql(sqlAndValues.sql, sqlAndValues.values, callback);
+ var self = this;
+
+ doOnSave(function() {
+ var sqlAndValues = self.driver.getUpdatePartialSql(model, id, data);
+ self._runSql(sqlAndValues.sql, sqlAndValues.values, callback);
+ });
},
chain: function(tasks, chainCallback) {
Please sign in to comment.
Something went wrong with that request. Please try again.