Support host / port in Remote connector

This commit is contained in:
Ritchie Martori 2014-02-21 15:03:45 -08:00
parent ac2206d257
commit 4c185e5453
3 changed files with 45 additions and 5 deletions

View File

@ -19,9 +19,17 @@ module.exports = RemoteConnector;
function RemoteConnector(settings) { function RemoteConnector(settings) {
assert(typeof settings === 'object', 'cannot initiaze RemoteConnector without a settings object'); assert(typeof settings === 'object', 'cannot initiaze RemoteConnector without a settings object');
this.client = settings.client; this.client = settings.client;
this.root = settings.root;
this.adapter = settings.adapter || 'rest'; this.adapter = settings.adapter || 'rest';
assert(this.root, 'RemoteConnector: settings.root is required'); this.protocol = settings.protocol || 'http'
this.root = settings.root || '';
this.host = settings.host || 'localhost';
this.port = settings.port || 3000;
if(settings.url) {
this.url = settings.url;
} else {
this.url = this.protocol + '://' + this.host + ':' + this.port + this.root;
}
// handle mixins here // handle mixins here
this.DataAccessObject = function() {}; this.DataAccessObject = function() {};
@ -40,7 +48,7 @@ RemoteConnector.initialize = function(dataSource, callback) {
RemoteConnector.prototype.define = function(definition) { RemoteConnector.prototype.define = function(definition) {
var Model = definition.model; var Model = definition.model;
var className = compat.getClassNameForRemoting(Model); var className = compat.getClassNameForRemoting(Model);
var url = this.root; var url = this.url;
var adapter = this.adapter; var adapter = this.adapter;
Model.remotes(function(err, remotes) { Model.remotes(function(err, remotes) {

View File

@ -253,8 +253,6 @@ Model.createProxyMethod = function createProxyFunction(remoteMethod) {
var callback; var callback;
if(lastArgIsFunc) { if(lastArgIsFunc) {
callback = args.pop(); callback = args.pop();
} else {
callback = noop;
} }
Model.remotes(function(err, remotes) { Model.remotes(function(err, remotes) {
@ -265,6 +263,7 @@ Model.createProxyMethod = function createProxyFunction(remoteMethod) {
for(var key in original) { for(var key in original) {
fn[key] = original[key]; fn[key] = original[key];
} }
fn._delegate = true;
} }
// setup the initial model // setup the initial model

View File

@ -0,0 +1,33 @@
var loopback = require('../');
describe('RemoteConnector', function() {
beforeEach(function(done) {
var LocalModel = this.LocalModel = loopback.DataModel.extend('LocalModel');
var RemoteModel = loopback.DataModel.extend('LocalModel');
var localApp = loopback();
var remoteApp = loopback();
localApp.model(LocalModel);
remoteApp.model(RemoteModel);
remoteApp.use(loopback.rest());
RemoteModel.attachTo(loopback.memory());
remoteApp.listen(0, function() {
var ds = loopback.createDataSource({
host: remoteApp.get('host'),
port: remoteApp.get('port'),
connector: loopback.Remote
});
LocalModel.attachTo(ds);
done();
});
});
it('should alow methods to be called remotely', function (done) {
var data = {foo: 'bar'};
this.LocalModel.create(data, function(err, result) {
if(err) return done(err);
expect(result).to.deep.equal({id: 1, foo: 'bar'});
done();
});
});
});