Skip to content

Commit

Permalink
Added current connection as param passed to onSave
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Brady committed Dec 13, 2012
1 parent 2be52f5 commit acc903c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -582,19 +582,20 @@ __Example__
}); });


<a name="modelOnSave" /> <a name="modelOnSave" />
### Model.onSave(obj, callback) ### Model.onSave(obj, connection, callback)


If preset this function will be called when an update or save occures. You would typically create this method If preset this function will be called when an update or save occures. You would typically create this method
in your model file. in your model file.


__Arguments__ __Arguments__


* obj - The object or partial object, in the case of [update](#modelUpdate), being saved. * 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 * callback() - The callback to be called when the onSave is complete


__Example__ __Example__


Person.onSave = function(obj, callback) { Person.onSave = function(obj, connection, callback) {
obj.lastUpdated = new Date(); obj.lastUpdated = new Date();
callback(); callback();
}; };
Expand Down
2 changes: 1 addition & 1 deletion examples/webapp/models/blog.js
Expand Up @@ -13,7 +13,7 @@ module.exports = Blog = persist.define("Blog", {
.hasOne(Category) .hasOne(Category)
.hasMany(Keyword, { through: "blogs_keywords" }); .hasMany(Keyword, { through: "blogs_keywords" });


Blog.onSave = function(obj, callback) { Blog.onSave = function(obj, connection, callback) {
obj.lastUpdated = new Date(); obj.lastUpdated = new Date();
callback(); callback();
} }
8 changes: 4 additions & 4 deletions lib/connection.js
Expand Up @@ -83,7 +83,7 @@ var Connection = Class.extend({


function doOnSave(callback) { function doOnSave(callback) {
if (obj._getModel().onSave) { if (obj._getModel().onSave) {
return obj._getModel().onSave(obj, callback); return obj._getModel().onSave(obj, self, callback);
} }


callback(); callback();
Expand Down Expand Up @@ -135,7 +135,7 @@ var Connection = Class.extend({
updatePartial: function(model, id, data, callback) { updatePartial: function(model, id, data, callback) {
function doOnSave(callback) { function doOnSave(callback) {
if (model.onSave) { if (model.onSave) {
return model.onSave(data, callback); return model.onSave(data, self, callback);
} }


callback(); callback();
Expand All @@ -144,8 +144,8 @@ var Connection = Class.extend({
var self = this; var self = this;


doOnSave(function() { doOnSave(function() {
var sqlAndValues = this.driver.getUpdatePartialSql(model, id, data); var sqlAndValues = self.driver.getUpdatePartialSql(model, id, data);
this._runSql(sqlAndValues.sql, sqlAndValues.values, callback); self._runSql(sqlAndValues.sql, sqlAndValues.values, callback);
}); });
}, },


Expand Down

0 comments on commit acc903c

Please sign in to comment.