loopback/lib/application.js

98 lines
1.7 KiB
JavaScript
Raw Normal View History

2013-05-01 19:11:43 +00:00
/**
* Module dependencies.
*/
var Model = require('../node_modules/model/lib/model')
, DataSource = require('../node_modules/data-source')
2013-05-23 16:53:42 +00:00
, assert = require('assert')
, 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.
*/
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.
*/
app.models = {};
/**
* Define a model.
*
* @param name {String}
* @param options {Object}
* @returns {Model}
*/
2013-05-23 16:53:42 +00:00
2013-05-23 03:41:56 +00:00
app.defineModel =
2013-05-01 19:11:43 +00:00
app.define = function (name, options) {
2013-05-23 16:53:42 +00:00
var remotes = this.remotes();
2013-05-01 19:11:43 +00:00
options = options || {};
options.name = options.name || name;
BaseModel = options.extend || Model;
assert(options.name, 'a name is required to define a model');
2013-05-23 16:53:42 +00:00
var ModelCtor = (this.model[name] = BaseModel.extend(options));
var proto = ModelCtor.prototype;
remotes.exports[name] = ModelCtor;
// default shared methods
if(ModelCtor.all) ModelCtor.all.shared = true;
if(proto.save) proto.save = true;
// attach a remotes reference
ModelCtor.remotes = remotes;
return ModelCtor;
2013-05-01 19:11:43 +00:00
}
/**
* App data sources.
*/
app.dataSources = {};
/**
* Attach a remote data source.
*
* @param name {String}
* @param options {Object}
* @returns {DataSource}
*/
app.dataSource = function (name, options) {
return (app[name] = new DataSource(options));
}