loopback/lib/application.js

233 lines
5.3 KiB
JavaScript
Raw Normal View History

2013-05-01 19:11:43 +00:00
/**
* Module dependencies.
*/
var Model = require('../node_modules/model/lib/model')
2013-05-24 14:59:23 +00:00
, 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);
}
}
}
}
2013-05-24 14:59:23 +00:00
/**
2013-05-24 15:57:52 +00:00
* Get ModelBuilder.
2013-05-24 14:59:23 +00:00
*/
2013-05-24 15:57:52 +00:00
app.modelBuilder = function () {
2013-05-28 15:53:40 +00:00
return this._modelBuilder || (this._modelBuilder = new ModelBuilder())
2013-05-24 14:59:23 +00:00
}
2013-05-01 19:11:43 +00:00
/**
* Define a model.
*
* @param name {String}
* @param options {Object}
* @returns {Model}
*/
2013-05-24 14:59:23 +00:00
app.model =
2013-05-23 03:41:56 +00:00
app.defineModel =
2013-05-24 14:59:23 +00:00
app.define = function (name, properties, options) {
2013-05-24 15:57:52 +00:00
var modelBuilder = this.modelBuilder();
var ModelCtor = modelBuilder.define(name, properties, options);
2013-05-01 19:11:43 +00:00
2013-05-24 14:59:23 +00:00
ModelCtor.dataSource = function (name) {
var dataSource = app.dataSources[name];
dataSource.attach(this);
2013-05-24 22:08:23 +00:00
var hasMany = ModelCtor.hasMany;
2013-06-05 16:35:35 +00:00
if(!hasMany) return;
2013-05-24 22:08:23 +00:00
// override the default relations to add shared proxy methods
// cannot expose the relation methods since they are only defined
// once you get them (eg. prototype[name])
ModelCtor.hasMany = function (anotherClass, params) {
var origArgs = arguments;
var thisClass = this, thisClassName = this.modelName;
params = params || {};
if (typeof anotherClass === 'string') {
params.as = anotherClass;
if (params.model) {
anotherClass = params.model;
} else {
var anotherClassName = i8n.singularize(anotherClass).toLowerCase();
for(var name in this.schema.models) {
if (name.toLowerCase() === anotherClassName) {
anotherClass = this.schema.models[name];
}
}
}
}
var pluralized = i8n.pluralize(anotherClass.modelName);
var methodName = params.as ||
i8n.camelize(pluralized, true);
var proxyMethodName = 'get' + i8n.titleize(pluralized, true);
// create a proxy method
var fn = this.prototype[proxyMethodName] = function () {
// this cannot be a shared method
// because it is defined when you
2013-06-05 16:35:35 +00:00
// inside a property getter...
2013-05-24 22:08:23 +00:00
this[methodName].apply(thisClass, arguments);
};
2013-06-05 16:35:35 +00:00
2013-05-24 22:08:23 +00:00
fn.shared = true;
fn.http = {verb: 'get', path: '/' + methodName};
hasMany.apply(this, arguments);
};
2013-05-24 14:59:23 +00:00
};
2013-05-23 16:53:42 +00:00
2013-05-24 22:08:23 +00:00
ModelCtor.shared = true;
2013-06-08 01:20:24 +00:00
ModelCtor.sharedCtor = function (data, id, fn) {
if(typeof data === 'function') {
fn = data;
data = null;
id = null;
} else if (typeof id === 'function') {
fn = id;
id = null;
}
2013-06-08 01:31:29 +00:00
if(id && data) {
var model = new ModelCtor(data);
model.id = id;
fn(null, model);
} else if(data) {
2013-06-08 01:20:24 +00:00
fn(null, new ModelCtor(data));
} else if(id) {
2013-06-05 16:35:35 +00:00
ModelCtor.find(id, fn);
} else {
2013-06-08 01:20:24 +00:00
fn(new Error('must specify an id or data'));
2013-06-05 16:35:35 +00:00
}
2013-05-24 14:59:23 +00:00
};
2013-05-24 22:08:23 +00:00
ModelCtor.sharedCtor.accepts = [
2013-06-08 01:20:24 +00:00
{arg: 'data', type: 'object'},
{arg: 'id', type: 'any'}
2013-05-24 22:08:23 +00:00
];
ModelCtor.sharedCtor.http = [
{path: '/'},
{path: '/:id'}
];
2013-05-23 16:53:42 +00:00
2013-05-24 14:59:23 +00:00
return (app._models[ModelCtor.pluralModelName] = ModelCtor);
}
/**
* Get all models.
*/
app.models = function () {
var models = this._models;
2013-06-05 16:35:35 +00:00
var result = {};
2013-05-24 14:59:23 +00:00
var dataSources = this.dataSources;
2013-05-23 16:53:42 +00:00
2013-05-24 14:59:23 +00:00
// add in any model from a data source
Object.keys(this.dataSources).forEach(function (name) {
var dataSource = dataSources[name];
Object.keys(dataSource.models).forEach(function (className) {
var model = dataSource.models[className];
2013-06-05 16:35:35 +00:00
result[exportedName(model)] = model;
2013-05-24 14:59:23 +00:00
});
});
2013-05-23 16:53:42 +00:00
2013-06-05 16:35:35 +00:00
// add in defined models
Object.keys(models).forEach(function (name) {
var model = models[name];
result[exportedName(model)] = model;
});
function exportedName(model) {
return model.pluralModelName || i8n.pluralize(model.modelName);
}
return result;
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
Object.keys(models)
.forEach(function (name) {
var ModelCtor = models[name];
// only add shared models
if(ModelCtor.shared && typeof ModelCtor.sharedCtor === 'function') {
result[name] = ModelCtor;
}
});
return result;
}
2013-05-01 19:11:43 +00:00
/**
2013-05-24 14:59:23 +00:00
* App data sources and models.
2013-05-01 19:11:43 +00:00
*/
2013-05-24 14:59:23 +00:00
app._models = {};
2013-05-01 19:11:43 +00:00
app.dataSources = {};
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
/**
* Attach a remote data source.
*
* @param name {String}
* @param options {Object}
* @returns {DataSource}
*/
app.dataSource = function (name, options) {
2013-05-24 14:59:23 +00:00
var dataSources = this.dataSources || (this.dataSources = {});
return (dataSources[name] = new DataSource(options.adapter, options));
2013-05-01 19:11:43 +00:00
}