Skip to content

Commit fabb0f4

Browse files
committed
Adds hook afterAutoFetch triggered after extending and auto fetching (if any) associations (#219)
1 parent 35d6657 commit fabb0f4

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ will be called when that event happens.
373373
Currently the following events are supported:
374374

375375
- `afterLoad` : (no parameters) Right after loading and preparing an instance to be used;
376+
- `afterAutoFetch` : (no parameters) Right after auto-fetching associations (if any), it will trigger regardless of having associations or not;
376377
- `beforeSave` : (no parameters) Right before trying to save;
377378
- `afterSave` : (bool success) Right after saving;
378379
- `beforeCreate` : (no parameters) Right before trying to save a new instance (prior to `beforeSave`);

lib/Model.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ var Singleton = require("./Singleton");
99
var Utilities = require("./Utilities");
1010
var Validators = require("./Validators");
1111
var ErrorCodes = require("./ErrorCodes");
12+
var Hook = require("./Hook");
1213
var AvailableHooks = [
1314
"beforeCreate", "afterCreate",
1415
"beforeSave", "afterSave",
1516
"beforeValidation",
16-
"beforeRemove", "afterRemove"
17+
"beforeRemove", "afterRemove",
18+
"afterLoad",
19+
"afterAutoFetch"
1720
];
1821

1922
exports.Model = Model;
@@ -126,10 +129,12 @@ function Model(opts) {
126129
autoFetchLimit : inst_opts.autoFetchLimit,
127130
cascadeRemove : inst_opts.cascadeRemove
128131
}, function () {
129-
if (--pending > 0) return;
130-
if (typeof cb == "function") {
131-
return cb(instance);
132-
}
132+
Hook.wait(instance, opts.hooks.afterAutoFetch, function (err) {
133+
if (--pending > 0) return;
134+
if (typeof cb == "function") {
135+
return cb(instance);
136+
}
137+
});
133138
});
134139
});
135140
});

test/integration2/hook.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,48 @@ describe("Hook", function() {
397397
});
398398
});
399399

400+
describe("afterAutoFetch", function () {
401+
var afterAutoFetch = false;
402+
403+
before(setup({
404+
afterAutoFetch: function () {
405+
afterAutoFetch = true;
406+
}
407+
}));
408+
409+
it("should trigger when defining a model", function (done) {
410+
var John = new Person({ name: "John" });
411+
412+
afterAutoFetch.should.be.true;
413+
414+
return done();
415+
});
416+
417+
describe("if hook method has 1 argument", function () {
418+
var afterAutoFetch = false;
419+
420+
before(setup({
421+
afterAutoFetch : function (next) {
422+
setTimeout(function () {
423+
afterAutoFetch = true;
424+
425+
return next();
426+
}.bind(this), 200);
427+
}
428+
}));
429+
430+
it("should wait for hook to finish", function (done) {
431+
this.timeout(500);
432+
433+
Person.create([{ name: "John Doe" }], function (err, items) {
434+
afterAutoFetch.should.be.true;
435+
436+
return done();
437+
});
438+
});
439+
});
440+
});
441+
400442
describe("beforeRemove", function () {
401443
before(setup());
402444

0 commit comments

Comments
 (0)