Skip to content

Commit

Permalink
Adds hook afterAutoFetch triggered after extending and auto fetching …
Browse files Browse the repository at this point in the history
…(if any) associations (#219)
  • Loading branch information
dresende committed Jul 5, 2013
1 parent 35d6657 commit fabb0f4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ will be called when that event happens.
Currently the following events are supported:

- `afterLoad` : (no parameters) Right after loading and preparing an instance to be used;
- `afterAutoFetch` : (no parameters) Right after auto-fetching associations (if any), it will trigger regardless of having associations or not;
- `beforeSave` : (no parameters) Right before trying to save;
- `afterSave` : (bool success) Right after saving;
- `beforeCreate` : (no parameters) Right before trying to save a new instance (prior to `beforeSave`);
Expand Down
15 changes: 10 additions & 5 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ var Singleton = require("./Singleton");
var Utilities = require("./Utilities");
var Validators = require("./Validators");
var ErrorCodes = require("./ErrorCodes");
var Hook = require("./Hook");
var AvailableHooks = [
"beforeCreate", "afterCreate",
"beforeSave", "afterSave",
"beforeValidation",
"beforeRemove", "afterRemove"
"beforeRemove", "afterRemove",
"afterLoad",
"afterAutoFetch"
];

exports.Model = Model;
Expand Down Expand Up @@ -126,10 +129,12 @@ function Model(opts) {
autoFetchLimit : inst_opts.autoFetchLimit,
cascadeRemove : inst_opts.cascadeRemove
}, function () {
if (--pending > 0) return;
if (typeof cb == "function") {
return cb(instance);
}
Hook.wait(instance, opts.hooks.afterAutoFetch, function (err) {
if (--pending > 0) return;
if (typeof cb == "function") {
return cb(instance);
}
});
});
});
});
Expand Down
42 changes: 42 additions & 0 deletions test/integration2/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,48 @@ describe("Hook", function() {
});
});

describe("afterAutoFetch", function () {
var afterAutoFetch = false;

before(setup({
afterAutoFetch: function () {
afterAutoFetch = true;
}
}));

it("should trigger when defining a model", function (done) {
var John = new Person({ name: "John" });

afterAutoFetch.should.be.true;

return done();
});

describe("if hook method has 1 argument", function () {
var afterAutoFetch = false;

before(setup({
afterAutoFetch : function (next) {
setTimeout(function () {
afterAutoFetch = true;

return next();
}.bind(this), 200);
}
}));

it("should wait for hook to finish", function (done) {
this.timeout(500);

Person.create([{ name: "John Doe" }], function (err, items) {
afterAutoFetch.should.be.true;

return done();
});
});
});
});

describe("beforeRemove", function () {
before(setup());

Expand Down

0 comments on commit fabb0f4

Please sign in to comment.