Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added afterSave hook

  • Loading branch information...
commit 10bc5076d028b1d5c7bc0045c6d58492aebddcb9 1 parent f12e51d
Scott Burch authored
6 Base.js
View
@@ -35,7 +35,11 @@ module.exports = function(db, name, config) {
that.id = res.id;
that.rev = res.rev
}
- callback(err, res);
+ if(err) {
+ callback(err, res);
+ } else {
+ that.afterSave ? that.afterSave(res, callback) : callback(err, res);
+ }
});
}
11 README.md
View
@@ -82,7 +82,12 @@ Next, create your domain files in ../testDomain like this:
}, function(that) {
this.beforeSave = function() {
that.fullName = that.firstName + ' ' + that.lastName;
- }
+ },
+ that.afterSave = function(res, callback) {
+ // do something and callback with (err, res)
+ callback(undefined, res);
+ };
+
return that;
});
@@ -106,6 +111,10 @@ After running init() the domain constructors are available from the couch-ar obj
domain constructors can include a hook beforeSave() that will be run before a document
is saved or updated in the DB.
+## After Hooks
+
+domain constructors can include a hook afterSave(res, cb) that will run after a document is saved. See the previous example for the format.
+
## create()
14 test/spec/TestUserSpec.js
View
@@ -58,6 +58,19 @@ describe('couch-ar', function () {
asyncSpecWait();
});
+ it('should have called afterSave after saving and before calling the callback', function() {
+ expect(user.afterSaveRun).toBe(true);
+ });
+
+ it('should call afterSave method after writing to the db', function() {
+ domain.TestUser.findByUsername('tester1', function(u) {
+ expect(u.afterSaveRun).toBe(undefined);
+ asyncSpecDone();
+ });
+ asyncSpecWait();
+ });
+
+
it('should allow us to create more than one', function () {
var u = domain.TestUser.create({username:'tester2', firstName:'Test2', lastName:'Tester2', dbName:testConfig.domainDbName});
u.save(function (err, res) {
@@ -96,6 +109,7 @@ describe('couch-ar', function () {
})
asyncSpecWait();
});
+
});
describe('findByUsername() method', function () {
7 test/testDomain/TestUser.js
View
@@ -26,6 +26,13 @@ domain.create('TestUser',{
this.beforeSave = function() {
that.fullName = that.firstName + ' ' + that.lastName;
}
+ that.afterSave = function(res, callback) {
+ setTimeout(function() {
+ that.afterSaveRun = true;
+ callback(undefined, res);
+ },200);
+ };
+
});
10 test/testDomain2/testUser.js
View
@@ -7,7 +7,8 @@ domain.create('TestUser',{
password: {},
firstName:{},
lastName: {},
- fullName: {finders:false}
+ fullName: {finders:false},
+ afterSaveRun: {}
},
hasMany: {
phoneNumbers: 'PhoneNumber',
@@ -26,6 +27,13 @@ domain.create('TestUser',{
that.beforeSave = function() {
that.fullName = that.firstName + ' ' + that.lastName;
};
+
+ that.afterSave = function(res, callback) {
+ setTimeout(function() {
+ that.afterSaveRun = true;
+ callback(undefined, res);
+ },200);
+ };
return that;
});
Please sign in to comment.
Something went wrong with that request. Please try again.