Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added afterSave hook

  • Loading branch information...
commit 10bc5076d028b1d5c7bc0045c6d58492aebddcb9 1 parent f12e51d
Scott Burch authored
6 Base.js
@@ -35,7 +35,11 @@ module.exports = function(db, name, config) {
35 35 that.id = res.id;
36 36 that.rev = res.rev
37 37 }
38   - callback(err, res);
  38 + if(err) {
  39 + callback(err, res);
  40 + } else {
  41 + that.afterSave ? that.afterSave(res, callback) : callback(err, res);
  42 + }
39 43 });
40 44 }
41 45
11 README.md
Source Rendered
@@ -82,7 +82,12 @@ Next, create your domain files in ../testDomain like this:
82 82 }, function(that) {
83 83 this.beforeSave = function() {
84 84 that.fullName = that.firstName + ' ' + that.lastName;
85   - }
  85 + },
  86 + that.afterSave = function(res, callback) {
  87 + // do something and callback with (err, res)
  88 + callback(undefined, res);
  89 + };
  90 +
86 91 return that;
87 92 });
88 93
@@ -106,6 +111,10 @@ After running init() the domain constructors are available from the couch-ar obj
106 111 domain constructors can include a hook beforeSave() that will be run before a document
107 112 is saved or updated in the DB.
108 113
  114 +## After Hooks
  115 +
  116 +domain constructors can include a hook afterSave(res, cb) that will run after a document is saved. See the previous example for the format.
  117 +
109 118
110 119 ## create()
111 120
14 test/spec/TestUserSpec.js
@@ -58,6 +58,19 @@ describe('couch-ar', function () {
58 58 asyncSpecWait();
59 59 });
60 60
  61 + it('should have called afterSave after saving and before calling the callback', function() {
  62 + expect(user.afterSaveRun).toBe(true);
  63 + });
  64 +
  65 + it('should call afterSave method after writing to the db', function() {
  66 + domain.TestUser.findByUsername('tester1', function(u) {
  67 + expect(u.afterSaveRun).toBe(undefined);
  68 + asyncSpecDone();
  69 + });
  70 + asyncSpecWait();
  71 + });
  72 +
  73 +
61 74 it('should allow us to create more than one', function () {
62 75 var u = domain.TestUser.create({username:'tester2', firstName:'Test2', lastName:'Tester2', dbName:testConfig.domainDbName});
63 76 u.save(function (err, res) {
@@ -96,6 +109,7 @@ describe('couch-ar', function () {
96 109 })
97 110 asyncSpecWait();
98 111 });
  112 +
99 113 });
100 114
101 115 describe('findByUsername() method', function () {
7 test/testDomain/TestUser.js
@@ -26,6 +26,13 @@ domain.create('TestUser',{
26 26 this.beforeSave = function() {
27 27 that.fullName = that.firstName + ' ' + that.lastName;
28 28 }
  29 + that.afterSave = function(res, callback) {
  30 + setTimeout(function() {
  31 + that.afterSaveRun = true;
  32 + callback(undefined, res);
  33 + },200);
  34 + };
  35 +
29 36 });
30 37
31 38
10 test/testDomain2/testUser.js
@@ -7,7 +7,8 @@ domain.create('TestUser',{
7 7 password: {},
8 8 firstName:{},
9 9 lastName: {},
10   - fullName: {finders:false}
  10 + fullName: {finders:false},
  11 + afterSaveRun: {}
11 12 },
12 13 hasMany: {
13 14 phoneNumbers: 'PhoneNumber',
@@ -26,6 +27,13 @@ domain.create('TestUser',{
26 27 that.beforeSave = function() {
27 28 that.fullName = that.firstName + ' ' + that.lastName;
28 29 };
  30 +
  31 + that.afterSave = function(res, callback) {
  32 + setTimeout(function() {
  33 + that.afterSaveRun = true;
  34 + callback(undefined, res);
  35 + },200);
  36 + };
29 37 return that;
30 38 });
31 39

0 comments on commit 10bc507

Please sign in to comment.
Something went wrong with that request. Please try again.