2013-05-01 19:11:43 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2013-07-16 19:03:41 +00:00
|
|
|
var DataSource = require('loopback-data').DataSource
|
|
|
|
, ModelBuilder = require('loopback-data').ModelBuilder
|
2013-05-23 16:53:42 +00:00
|
|
|
, assert = require('assert')
|
2013-06-13 04:30:20 +00:00
|
|
|
, RemoteObjects = require('sl-remoting');
|
2013-05-01 19:11:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export the app prototype.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var app = exports = module.exports = {};
|
|
|
|
|
|
|
|
/**
|
2013-05-23 16:53:42 +00:00
|
|
|
* Create a set of remote objects.
|
|
|
|
*/
|
2013-06-17 15:01:22 +00:00
|
|
|
|
2013-05-23 16:53:42 +00:00
|
|
|
app.remotes = function () {
|
|
|
|
if(this._remotes) {
|
|
|
|
return this._remotes;
|
|
|
|
} else {
|
|
|
|
return (this._remotes = RemoteObjects.create());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-03 00:04:20 +00:00
|
|
|
/**
|
|
|
|
* Expose an object or Class remotely.
|
|
|
|
*
|
|
|
|
* @param {String} name The remote namespace (eg. url base)
|
|
|
|
* @param {Object|Function} obj The object to remote
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.remote = function (name, obj) {
|
|
|
|
// add the object to the remote exports
|
|
|
|
this.remotes().exports[name] = obj;
|
|
|
|
|
|
|
|
// clear the handlers cache
|
|
|
|
this._handlers = {};
|
|
|
|
}
|
|
|
|
|
2013-05-23 16:53:42 +00:00
|
|
|
/**
|
|
|
|
* Remove a route by reference.
|
2013-05-01 19:11:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
app.disuse = function (route) {
|
|
|
|
if(this.stack) {
|
|
|
|
for (var i = 0; i < this.stack.length; i++) {
|
|
|
|
if(this.stack[i].route === route) {
|
|
|
|
this.stack.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* App models.
|
|
|
|
*/
|
|
|
|
|
2013-06-06 00:11:21 +00:00
|
|
|
app._models = [];
|
2013-07-03 00:22:51 +00:00
|
|
|
app._services = [];
|
2013-05-01 19:11:43 +00:00
|
|
|
|
2013-05-24 14:59:23 +00:00
|
|
|
/**
|
2013-06-06 00:11:21 +00:00
|
|
|
* Expose a model.
|
2013-05-01 19:11:43 +00:00
|
|
|
*
|
2013-06-06 00:11:21 +00:00
|
|
|
* @param Model {Model}
|
2013-05-01 19:11:43 +00:00
|
|
|
*/
|
2013-05-24 14:59:23 +00:00
|
|
|
|
2013-06-06 00:11:21 +00:00
|
|
|
app.model = function (Model) {
|
2013-07-01 17:41:52 +00:00
|
|
|
var remotes = this.remotes();
|
|
|
|
|
2013-06-06 00:11:21 +00:00
|
|
|
this._models.push(Model);
|
2013-06-20 17:17:55 +00:00
|
|
|
Model.shared = true;
|
2013-06-12 22:44:38 +00:00
|
|
|
Model.app = this;
|
2013-07-02 23:51:38 +00:00
|
|
|
Model.emit('attached', this);
|
2013-05-24 14:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-06 00:11:21 +00:00
|
|
|
* Get all exposed models.
|
2013-05-24 14:59:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
app.models = function () {
|
2013-06-06 00:11:21 +00:00
|
|
|
return this._models;
|
2013-05-01 19:11:43 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 22:08:23 +00:00
|
|
|
/**
|
2013-07-03 00:04:20 +00:00
|
|
|
* Expose a service.
|
|
|
|
*
|
|
|
|
* @param {String} name
|
|
|
|
* @param {Service} service
|
2013-05-24 22:08:23 +00:00
|
|
|
*/
|
|
|
|
|
2013-07-03 00:04:20 +00:00
|
|
|
app.service = function (name, service) {
|
|
|
|
this._services.push(service);
|
|
|
|
service.shared = true;
|
2013-05-24 22:08:23 +00:00
|
|
|
|
2013-07-03 00:04:20 +00:00
|
|
|
service.app = this;
|
|
|
|
|
|
|
|
// add to the remote exports
|
|
|
|
this.remote(name, service);
|
2013-05-24 14:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-03 00:22:51 +00:00
|
|
|
* Get all exposed services.
|
2013-05-24 14:59:23 +00:00
|
|
|
*/
|
|
|
|
|
2013-07-03 00:22:51 +00:00
|
|
|
app.services = function () {
|
|
|
|
return this._services;
|
2013-05-24 22:08:23 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 14:59:23 +00:00
|
|
|
/**
|
|
|
|
* Get the apps set of remote objects.
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.remotes = function () {
|
|
|
|
return this._remotes || (this._remotes = RemoteObjects.create());
|
2013-07-01 17:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a remotes handler.
|
2013-05-24 22:08:23 +00:00
|
|
|
*/
|
|
|
|
|
2013-07-01 17:41:52 +00:00
|
|
|
app.handler = function (type) {
|
|
|
|
var handler = this._handlers[type];
|
2013-05-24 22:08:23 +00:00
|
|
|
|
2013-07-01 17:41:52 +00:00
|
|
|
if(!handler) {
|
|
|
|
// get the sl remoting object
|
|
|
|
var remotes = this.remotes();
|
|
|
|
|
|
|
|
// create and save the handler
|
|
|
|
handler = this._handlers[type] = remotes.handler(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler;
|
2013-05-24 22:08:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-01 17:41:52 +00:00
|
|
|
/*!
|
|
|
|
* Handlers
|
2013-05-24 14:59:23 +00:00
|
|
|
*/
|
2013-07-01 17:41:52 +00:00
|
|
|
|
2013-07-03 00:22:51 +00:00
|
|
|
app._handlers = {};
|
|
|
|
|