Merge pull request #44 from doublemarked/39-support-promises
Add promise support
This commit is contained in:
commit
8343a64919
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var remoting = require('strong-remoting');
|
var remoting = require('strong-remoting');
|
||||||
|
var utils = require('loopback-datasource-juggler/lib/utils');
|
||||||
var jutil = require('loopback-datasource-juggler/lib/jutil');
|
var jutil = require('loopback-datasource-juggler/lib/jutil');
|
||||||
var RelationMixin = require('./relations');
|
var RelationMixin = require('./relations');
|
||||||
var InclusionMixin = require('loopback-datasource-juggler/lib/include');
|
var InclusionMixin = require('loopback-datasource-juggler/lib/include');
|
||||||
|
@ -94,14 +95,18 @@ function createProxyMethod(Model, remotes, remoteMethod) {
|
||||||
var callback;
|
var callback;
|
||||||
if (lastArgIsFunc) {
|
if (lastArgIsFunc) {
|
||||||
callback = args.pop();
|
callback = args.pop();
|
||||||
|
} else {
|
||||||
|
callback = utils.createPromiseCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (remoteMethod.isStatic) {
|
if (remoteMethod.isStatic) {
|
||||||
return remotes.invoke(remoteMethod.stringName, args, callback);
|
remotes.invoke(remoteMethod.stringName, args, callback);
|
||||||
|
} else {
|
||||||
|
var ctorArgs = [this.id];
|
||||||
|
remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
var ctorArgs = [this.id];
|
return callback.promise;
|
||||||
return remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scope[remoteMethod.name] = remoteMethodProxy;
|
scope[remoteMethod.name] = remoteMethodProxy;
|
||||||
|
|
|
@ -94,6 +94,15 @@ describe('Model tests', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Model methods', function() {
|
||||||
|
it('should support promises', function(done) {
|
||||||
|
assert(User.create() instanceof Promise);
|
||||||
|
assert(User.find() instanceof Promise);
|
||||||
|
assert(User.findById(99) instanceof Promise);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Model.create([data], [callback])', function() {
|
describe('Model.create([data], [callback])', function() {
|
||||||
it('should create an instance and save to the attached data source',
|
it('should create an instance and save to the attached data source',
|
||||||
function(done) {
|
function(done) {
|
||||||
|
|
Loading…
Reference in New Issue