2013-05-01 19:11:43 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2013-06-07 20:12:46 +00:00
|
|
|
var DataSource = require('jugglingdb').DataSource
|
2013-05-24 15:57:52 +00:00
|
|
|
, ModelBuilder = require('jugglingdb').ModelBuilder
|
2013-05-23 16:53:42 +00:00
|
|
|
, assert = require('assert')
|
2013-05-24 14:59:23 +00:00
|
|
|
, RemoteObjects = require('sl-remoting')
|
|
|
|
, i8n = require('inflection');
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.remotes = function () {
|
|
|
|
if(this._remotes) {
|
|
|
|
return this._remotes;
|
|
|
|
} else {
|
|
|
|
return (this._remotes = RemoteObjects.create());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-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) {
|
|
|
|
this._models.push(Model);
|
2013-06-12 22:44:38 +00:00
|
|
|
Model.app = 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
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all remote objects.
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.remoteObjects = function () {
|
|
|
|
var result = {};
|
|
|
|
var models = this.models();
|
|
|
|
|
|
|
|
// add in models
|
2013-06-12 22:44:38 +00:00
|
|
|
models.forEach(function (ModelCtor) {
|
|
|
|
// only add shared models
|
|
|
|
if(ModelCtor.shared && typeof ModelCtor.sharedCtor === 'function') {
|
|
|
|
result[ModelCtor.pluralModelName] = ModelCtor;
|
|
|
|
}
|
|
|
|
});
|
2013-05-24 22:08:23 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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-05-01 19:11:43 +00:00
|
|
|
}
|