Refactor Model into class. Make createModel() just sugar.
This commit is contained in:
parent
acfaee2fb0
commit
3f7e4b693a
|
@ -151,95 +151,7 @@ asteroid.createDataSource = function (name, options) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
asteroid.createModel = function (name, properties, options) {
|
asteroid.createModel = function (name, properties, options) {
|
||||||
assert(typeof name === 'string', 'Cannot create a model without a name');
|
return asteroid.Model.extend(name, properties, options);
|
||||||
|
|
||||||
var mb = new ModelBuilder();
|
|
||||||
var ModelCtor = mb.define(name, properties, options);
|
|
||||||
|
|
||||||
ModelCtor.shared = true;
|
|
||||||
ModelCtor.sharedCtor = function (data, id, fn) {
|
|
||||||
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'));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ModelCtor.sharedCtor.accepts = [
|
|
||||||
{arg: 'data', type: 'object', http: {source: 'body'}},
|
|
||||||
{arg: 'id', type: 'any', http: {source: 'url'}}
|
|
||||||
];
|
|
||||||
|
|
||||||
ModelCtor.sharedCtor.http = [
|
|
||||||
{path: '/'},
|
|
||||||
{path: '/:id'}
|
|
||||||
];
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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(ModelCtor, args);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ModelCtor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -300,7 +212,7 @@ asteroid.memory = function (name) {
|
||||||
* Built in models / services
|
* Built in models / services
|
||||||
*/
|
*/
|
||||||
|
|
||||||
asteroid.Model = asteroid.createModel('model');
|
asteroid.Model = require('./models/model');
|
||||||
asteroid.Email = require('./models/email');
|
asteroid.Email = require('./models/email');
|
||||||
asteroid.User = require('./models/user');
|
asteroid.User = require('./models/user');
|
||||||
asteroid.Session = require('./models/session');
|
asteroid.Session = require('./models/session');
|
||||||
|
|
|
@ -0,0 +1,110 @@
|
||||||
|
/**
|
||||||
|
* Module Dependencies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var ModelBuilder = require('jugglingdb').ModelBuilder
|
||||||
|
var modeler = new ModelBuilder();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extends from the built in `asteroid.Model` type.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var Model = module.exports = modeler.define('model');
|
||||||
|
|
||||||
|
Model.shared = true;
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Setup extended models.
|
||||||
|
*/
|
||||||
|
|
||||||
|
Model.setup = function () {
|
||||||
|
var ModelCtor = this;
|
||||||
|
|
||||||
|
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();
|
Loading…
Reference in New Issue