2013-07-15 18:32:00 +00:00
|
|
|
/**
|
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
|
|
|
|
2013-07-15 21:07:17 +00:00
|
|
|
var ModelBuilder = require('jugglingdb').ModelBuilder;
|
2013-07-15 18:32:00 +00:00
|
|
|
var modeler = new ModelBuilder();
|
|
|
|
|
|
|
|
/**
|
2013-07-15 21:07:17 +00:00
|
|
|
* Define the built in asteroid.Model.
|
2013-07-15 18:32:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
var Model = module.exports = modeler.define('model');
|
|
|
|
|
|
|
|
Model.shared = true;
|
2013-07-15 21:07:17 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* For **remoting**. Construct objects when calling instance methods remotely.
|
|
|
|
*/
|
|
|
|
|
2013-07-15 18:32:00 +00:00
|
|
|
Model.sharedCtor = function (data, id, fn) {
|
|
|
|
var ModelCtor = this;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
var model = new ModelCtor(data);
|
|
|
|
model.id = id;
|
|
|
|
fn(null, model);
|
|
|
|
} else if(data) {
|
|
|
|
fn(null, new ModelCtor(data));
|
|
|
|
} else if(id) {
|
|
|
|
ModelCtor.find(id, function (err, model) {
|
|
|
|
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
|
|
|
|
Model.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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// after remote hook
|
|
|
|
Model.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-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
|
|
|
|
Model.acl = [];
|
|
|
|
|
2013-07-15 18:32:00 +00:00
|
|
|
ModelCtor.sharedCtor.accepts = [
|
|
|
|
{arg: 'data', type: 'object', http: {source: 'body'}},
|
|
|
|
{arg: 'id', type: 'any', http: {source: 'url'}}
|
|
|
|
];
|
|
|
|
|
|
|
|
ModelCtor.sharedCtor.http = [
|
|
|
|
{path: '/'},
|
|
|
|
{path: '/:id'}
|
|
|
|
];
|
|
|
|
|
|
|
|
return ModelCtor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the initial model
|
|
|
|
Model.setup();
|