2013-07-15 18:32:00 +00:00
|
|
|
/**
|
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
2013-11-19 00:13:40 +00:00
|
|
|
var loopback = require('../loopback');
|
2013-07-30 21:26:49 +00:00
|
|
|
var ModelBuilder = require('loopback-datasource-juggler').ModelBuilder;
|
2013-07-15 18:32:00 +00:00
|
|
|
var modeler = new ModelBuilder();
|
|
|
|
|
|
|
|
/**
|
2013-07-16 17:49:25 +00:00
|
|
|
* Define the built in loopback.Model.
|
2013-07-15 18:32:00 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-11 21:35:54 +00:00
|
|
|
var Model = module.exports = modeler.define('Model');
|
2013-07-15 18:32:00 +00:00
|
|
|
|
|
|
|
Model.shared = true;
|
2013-07-15 21:07:17 +00:00
|
|
|
|
2013-07-15 18:32:00 +00:00
|
|
|
/*!
|
2013-07-15 21:07:17 +00:00
|
|
|
* Called when a model is extended.
|
2013-07-15 18:32:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
Model.setup = function () {
|
|
|
|
var ModelCtor = this;
|
|
|
|
|
2013-07-15 21:07:17 +00:00
|
|
|
// each model has its
|
|
|
|
// own access control
|
|
|
|
// list
|
2013-07-16 03:14:04 +00:00
|
|
|
ModelCtor.acl = [];
|
2013-07-15 21:07:17 +00:00
|
|
|
|
2013-07-16 03:14:04 +00:00
|
|
|
ModelCtor.sharedCtor = function (data, id, fn) {
|
2013-07-16 02:45:27 +00:00
|
|
|
if(typeof data === 'function') {
|
|
|
|
fn = data;
|
|
|
|
data = null;
|
|
|
|
id = null;
|
|
|
|
} else if (typeof id === 'function') {
|
|
|
|
fn = id;
|
|
|
|
|
|
|
|
if(typeof data !== 'object') {
|
|
|
|
id = data;
|
|
|
|
data = null;
|
|
|
|
} else {
|
|
|
|
id = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(id && data) {
|
2013-07-16 19:51:59 +00:00
|
|
|
var model = new ModelCtor(data);
|
2013-07-16 02:45:27 +00:00
|
|
|
model.id = id;
|
|
|
|
fn(null, model);
|
|
|
|
} else if(data) {
|
2013-07-16 19:51:59 +00:00
|
|
|
fn(null, new ModelCtor(data));
|
2013-07-16 02:45:27 +00:00
|
|
|
} else if(id) {
|
2013-08-17 05:11:58 +00:00
|
|
|
ModelCtor.findById(id, function (err, model) {
|
2013-07-16 02:45:27 +00:00
|
|
|
if(err) {
|
|
|
|
fn(err);
|
|
|
|
} else if(model) {
|
|
|
|
fn(null, model);
|
|
|
|
} else {
|
|
|
|
err = new Error('could not find a model with id ' + id);
|
|
|
|
err.statusCode = 404;
|
|
|
|
|
|
|
|
fn(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
fn(new Error('must specify an id or data'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// before remote hook
|
|
|
|
ModelCtor.beforeRemote = function (name, fn) {
|
|
|
|
var self = this;
|
|
|
|
if(this.app) {
|
|
|
|
var remotes = this.app.remotes();
|
|
|
|
remotes.before(self.pluralModelName + '.' + name, function (ctx, next) {
|
|
|
|
fn(ctx, ctx.result, next);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var args = arguments;
|
|
|
|
this.once('attached', function () {
|
|
|
|
self.beforeRemote.apply(self, args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 17:25:02 +00:00
|
|
|
// after remote hook
|
|
|
|
ModelCtor.afterRemote = function (name, fn) {
|
|
|
|
var self = this;
|
|
|
|
if(this.app) {
|
|
|
|
var remotes = this.app.remotes();
|
|
|
|
remotes.after(self.pluralModelName + '.' + name, function (ctx, next) {
|
|
|
|
fn(ctx, ctx.result, next);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var args = arguments;
|
|
|
|
this.once('attached', function () {
|
|
|
|
self.afterRemote.apply(self, args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2013-08-16 23:39:13 +00:00
|
|
|
|
|
|
|
// Map the prototype method to /:id with data in the body
|
2013-07-15 18:32:00 +00:00
|
|
|
ModelCtor.sharedCtor.accepts = [
|
2013-08-16 23:39:13 +00:00
|
|
|
{arg: 'id', type: 'any', http: {source: 'path'}}
|
|
|
|
// {arg: 'instance', type: 'object', http: {source: 'body'}}
|
2013-07-15 18:32:00 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
ModelCtor.sharedCtor.http = [
|
|
|
|
{path: '/:id'}
|
|
|
|
];
|
2013-07-25 00:21:15 +00:00
|
|
|
|
|
|
|
ModelCtor.sharedCtor.returns = {root: true};
|
2013-07-25 22:27:18 +00:00
|
|
|
|
2013-07-15 18:32:00 +00:00
|
|
|
return ModelCtor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the initial model
|
2013-11-11 21:35:54 +00:00
|
|
|
Model.setup();
|