2014-02-19 19:44:16 +00:00
|
|
|
/**
|
|
|
|
* Dependencies.
|
|
|
|
*/
|
|
|
|
|
2014-04-30 00:17:49 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
var remoting = require('strong-remoting');
|
2014-05-06 20:31:23 +00:00
|
|
|
var DataAccessObject = require('loopback-datasource-juggler/lib/dao');
|
2014-02-19 19:44:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the RemoteConnector class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = RemoteConnector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an instance of the connector with the given `settings`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function RemoteConnector(settings) {
|
|
|
|
assert(typeof settings === 'object', 'cannot initiaze RemoteConnector without a settings object');
|
|
|
|
this.client = settings.client;
|
|
|
|
this.adapter = settings.adapter || 'rest';
|
2014-02-21 23:03:45 +00:00
|
|
|
this.protocol = settings.protocol || 'http'
|
|
|
|
this.root = settings.root || '';
|
|
|
|
this.host = settings.host || 'localhost';
|
|
|
|
this.port = settings.port || 3000;
|
2014-04-30 00:17:49 +00:00
|
|
|
this.remotes = remoting.create();
|
2014-02-21 23:03:45 +00:00
|
|
|
|
2014-05-06 20:31:23 +00:00
|
|
|
// TODO(ritch) make sure this name works with Model.getSourceId()
|
|
|
|
this.name = 'remote-connector';
|
|
|
|
|
2014-02-21 23:03:45 +00:00
|
|
|
if(settings.url) {
|
|
|
|
this.url = settings.url;
|
|
|
|
} else {
|
|
|
|
this.url = this.protocol + '://' + this.host + ':' + this.port + this.root;
|
|
|
|
}
|
2014-02-19 19:44:16 +00:00
|
|
|
|
2014-04-30 03:40:00 +00:00
|
|
|
// handle mixins in the define() method
|
|
|
|
var DAO = this.DataAccessObject = function() {};
|
2014-02-19 19:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoteConnector.prototype.connect = function() {
|
2014-04-30 00:17:49 +00:00
|
|
|
this.remotes.connect(this.url, this.adapter);
|
2014-02-19 19:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoteConnector.initialize = function(dataSource, callback) {
|
|
|
|
var connector = dataSource.connector = new RemoteConnector(dataSource.settings);
|
|
|
|
connector.connect();
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteConnector.prototype.define = function(definition) {
|
|
|
|
var Model = definition.model;
|
2014-05-19 22:56:26 +00:00
|
|
|
var remotes = this.remotes;
|
2014-04-30 00:17:49 +00:00
|
|
|
var SharedClass;
|
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
assert(Model.sharedClass, 'cannot attach ' + Model.modelName
|
|
|
|
+ ' to a remote connector without a Model.sharedClass');
|
|
|
|
|
|
|
|
remotes.addClass(Model.sharedClass);
|
|
|
|
|
|
|
|
Model
|
|
|
|
.sharedClass
|
|
|
|
.methods()
|
|
|
|
.forEach(function(remoteMethod) {
|
|
|
|
// TODO(ritch) more elegant way of ignoring a nested shared class
|
|
|
|
if(remoteMethod.name !== 'Change'
|
|
|
|
&& remoteMethod.name !== 'Checkpoint') {
|
|
|
|
createProxyMethod(Model, remotes, remoteMethod);
|
|
|
|
}
|
|
|
|
});
|
2014-02-19 19:44:16 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 00:17:49 +00:00
|
|
|
function createProxyMethod(Model, remotes, remoteMethod) {
|
|
|
|
var scope = remoteMethod.isStatic ? Model : Model.prototype;
|
|
|
|
var original = scope[remoteMethod.name];
|
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
scope[remoteMethod.name] = function remoteMethodProxy() {
|
2014-04-30 00:17:49 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var lastArgIsFunc = typeof args[args.length - 1] === 'function';
|
|
|
|
var callback;
|
|
|
|
if(lastArgIsFunc) {
|
|
|
|
callback = args.pop();
|
|
|
|
}
|
2014-05-19 22:56:26 +00:00
|
|
|
|
2014-04-30 00:17:49 +00:00
|
|
|
remotes.invoke(remoteMethod.stringName, args, callback);
|
|
|
|
}
|
2014-02-19 19:44:16 +00:00
|
|
|
}
|
2014-04-30 00:17:49 +00:00
|
|
|
|
2014-02-19 19:44:16 +00:00
|
|
|
function noop() {}
|