Skip to content

Commit d014b37

Browse files
committed
Adds .success() and .fail() to ChainFind (dresende#316)
This is just an initial code, we should improve in the future and make more parts of the code use the "Promise" (like Model.get)
1 parent 10d1d0f commit d014b37

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

lib/ChainFind.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
var _ = require("lodash");
22
var Singleton = require("./Singleton");
33
var ChainInstance = require("./ChainInstance");
4+
var Promise = require("./Promise").Promise;
45

56
module.exports = ChainFind;
67

78
function ChainFind(Model, opts) {
9+
var promise = null;
810
var chain = {
911
find: function () {
1012
var cb = null;
@@ -110,6 +112,20 @@ function ChainFind(Model, opts) {
110112
run: function (cb) {
111113
return this.all(cb);
112114
},
115+
success: function (cb) {
116+
if (!promise) {
117+
promise = new Promise();
118+
promise.handle(this.all);
119+
}
120+
return promise.success(cb);
121+
},
122+
fail: function (cb) {
123+
if (!promise) {
124+
promise = new Promise();
125+
promise.handle(this.all);
126+
}
127+
return promise.fail(cb);
128+
},
113129
all: function (cb) {
114130
opts.driver.find(opts.only, opts.table, opts.conditions, {
115131
limit : opts.limit,

lib/Promise.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
exports.Promise = Promise;
2+
3+
function Promise(opts) {
4+
opts = opts || {};
5+
6+
var success_cb = opts.success || null;
7+
var fail_cb = opts.fail || null;
8+
9+
return {
10+
handle: function (promise) {
11+
promise(function (err) {
12+
if (err) {
13+
if (fail_cb) fail_cb(err);
14+
} else {
15+
var args = Array.prototype.slice.call(arguments, 1);
16+
17+
if (success_cb) success_cb.apply(null, args);
18+
}
19+
});
20+
},
21+
success: function (cb) {
22+
success_cb = cb;
23+
return this;
24+
},
25+
fail: function (cb) {
26+
fail_cb = cb;
27+
return this;
28+
}
29+
};
30+
}

test/integration/model-find-chain.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,32 @@ describe("Model.find() chaining", function() {
461461
});
462462
});
463463
});
464+
465+
describe(".success()", function () {
466+
before(setup());
467+
468+
it("should return a Promise with .fail() method", function (done) {
469+
Person.find().success(function (people) {
470+
should(Array.isArray(people));
471+
472+
return done();
473+
}).fail(function (err) {
474+
// never called..
475+
});
476+
});
477+
});
478+
479+
describe(".fail()", function () {
480+
before(setup());
481+
482+
it("should return a Promise with .success() method", function (done) {
483+
Person.find().fail(function (err) {
484+
// never called..
485+
}).success(function (people) {
486+
should(Array.isArray(people));
487+
488+
return done();
489+
});
490+
});
491+
});
464492
});

0 commit comments

Comments
 (0)