diff --git a/lib/connectors/remote.js b/lib/connectors/remote.js index 203a7f87..065682f6 100644 --- a/lib/connectors/remote.js +++ b/lib/connectors/remote.js @@ -19,9 +19,17 @@ module.exports = RemoteConnector; function RemoteConnector(settings) { assert(typeof settings === 'object', 'cannot initiaze RemoteConnector without a settings object'); this.client = settings.client; - this.root = settings.root; 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 this.DataAccessObject = function() {}; @@ -40,7 +48,7 @@ RemoteConnector.initialize = function(dataSource, callback) { RemoteConnector.prototype.define = function(definition) { var Model = definition.model; var className = compat.getClassNameForRemoting(Model); - var url = this.root; + var url = this.url; var adapter = this.adapter; Model.remotes(function(err, remotes) { diff --git a/lib/models/model.js b/lib/models/model.js index 0170a198..b0c34250 100644 --- a/lib/models/model.js +++ b/lib/models/model.js @@ -253,8 +253,6 @@ Model.createProxyMethod = function createProxyFunction(remoteMethod) { var callback; if(lastArgIsFunc) { callback = args.pop(); - } else { - callback = noop; } Model.remotes(function(err, remotes) { @@ -265,6 +263,7 @@ Model.createProxyMethod = function createProxyFunction(remoteMethod) { for(var key in original) { fn[key] = original[key]; } + fn._delegate = true; } // setup the initial model diff --git a/test/remote-connector.test.js b/test/remote-connector.test.js new file mode 100644 index 00000000..de996b92 --- /dev/null +++ b/test/remote-connector.test.js @@ -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(); + }); + }); +});