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');
|
|
|
|
var compat = require('../compat');
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// handle mixins here
|
|
|
|
this.DataAccessObject = function() {};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
var className = compat.getClassNameForRemoting(Model);
|
2014-04-30 00:17:49 +00:00
|
|
|
var remotes = this.remotes
|
|
|
|
var SharedClass;
|
|
|
|
var classes;
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
remotes.exports[className] = Model;
|
2014-02-21 03:43:50 +00:00
|
|
|
|
2014-04-30 00:17:49 +00:00
|
|
|
classes = remotes.classes();
|
|
|
|
|
|
|
|
for(; i < classes.length; i++) {
|
|
|
|
SharedClass = classes[i];
|
|
|
|
if(SharedClass.name === className) {
|
|
|
|
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-04-16 14:33:17 +00:00
|
|
|
|
2014-04-30 00:17:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
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];
|
|
|
|
|
|
|
|
var fn = scope[remoteMethod.name] = function remoteMethodProxy() {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var lastArgIsFunc = typeof args[args.length - 1] === 'function';
|
|
|
|
var callback;
|
|
|
|
if(lastArgIsFunc) {
|
|
|
|
callback = args.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
remotes.invoke(remoteMethod.stringName, args, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var key in original) {
|
|
|
|
fn[key] = original[key];
|
|
|
|
}
|
|
|
|
fn._delegate = true;
|
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() {}
|