2014-02-19 19:44:16 +00:00
|
|
|
/**
|
|
|
|
* Dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert')
|
|
|
|
, compat = require('../compat')
|
|
|
|
, _ = require('underscore');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
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-02-21 03:43:50 +00:00
|
|
|
|
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-02-21 23:03:45 +00:00
|
|
|
var url = this.url;
|
2014-02-21 03:43:50 +00:00
|
|
|
var adapter = this.adapter;
|
|
|
|
|
|
|
|
Model.remotes(function(err, remotes) {
|
|
|
|
var sharedClass = getSharedClass(remotes, className);
|
|
|
|
remotes.connect(url, adapter);
|
|
|
|
sharedClass
|
|
|
|
.methods()
|
|
|
|
.forEach(Model.createProxyMethod.bind(Model));
|
|
|
|
});
|
2014-02-19 19:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSharedClass(remotes, className) {
|
|
|
|
return _.find(remotes.classes(), function(sharedClass) {
|
|
|
|
return sharedClass.name === className;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function noop() {}
|