Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Made onSave take a callback

  • Loading branch information...
commit 4de37021db0d8c657eb920065b32d5e47fac98ec 1 parent 62b7c8b
Will Brady authored
Showing with 39 additions and 32 deletions.
  1. +39 −32 lib/connection.js
View
71 lib/connection.js
@@ -81,45 +81,52 @@ var Connection = Class.extend({
}
}
- function doSave() {
+ function doOnSave(callback) {
if (obj._getModel().onSave) {
- obj._getModel().onSave(obj);
+ return obj._getModel().onSave(obj, 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);
+ });
});
});
}
Please sign in to comment.
Something went wrong with that request. Please try again.