Permalink
Browse files

Load up models on app-startup.

  • Loading branch information...
1 parent a0f33a2 commit 4aefaa9dacaa3fbd672b7f037768ee24ea9a6b11 @mde mde committed Nov 18, 2011
Showing with 60 additions and 34 deletions.
  1. +1 −1 README.md
  2. +5 −7 lib/base_controller.js
  3. +45 −19 lib/geddy.js
  4. +8 −6 lib/model/index.js
  5. +1 −1 templates/resource_model.ejs
View
@@ -229,7 +229,7 @@ Here is an example of a model with some validations:
// Do some other stuff
};
- geddy.model.registerModel('User', User);
+ User = geddy.model.registerModel('User', User);
Creating an instance of one of these models is easy:
View
@@ -535,10 +535,7 @@ controller.BaseController.prototype = new (function () {
to the layout to use to render the template for this response.
*/
this.respond = function (content, opts) {
- var c = content
- , r
- , headers
- , options = opts || {}
+ var options = opts || {}
, format = typeof opts == 'string' ? options : options.format
, negotiated = _negotiateContent.call(this, format);
@@ -558,16 +555,17 @@ controller.BaseController.prototype = new (function () {
}
// If content needs formatting
- if (typeof c != 'string') {
+ if (typeof content != 'string') {
if (this.format) {
// Special-case HTML -- will go out to template-rendering code,
// and then come back here with content as a string
if (this.format == 'html') {
this.renderTemplate(content);
return;
}
+ // Otherwise format according to ... format
else {
- c = controller.formatters[format](c, this);
+ content = controller.formatters[this.format](content, this);
}
}
// If we couldn't perform content-negotiaton successfully, bail
@@ -578,7 +576,7 @@ controller.BaseController.prototype = new (function () {
}
}
- this.content = c;
+ this.content = content;
_doResponse.apply(this, [200, {'Content-Type': this.contentType}]);
};
View
@@ -46,39 +46,63 @@ geddy.inflection = inflection;
geddy.model = model;
geddy.mixin(geddy, new (function () {
+ var JSPAT = /\.js$/;
- // Load controller ctors
- // ==================
- var _getControllerConstructors = function (next) {
- var dirname = '/app/controllers'
- , dirList = fs.readdirSync(dir + dirname)
+ var _getDirList = function (dirname) {
+ var dirList = fs.readdirSync(dir + dirname)
, fileName
, filePath
, ctorName
- , ctors = {}
- , ctor
- , jsPat = /\.js$/;
+ , ret = [];
- // Dynamically create controller constructors from files in constructors/
for (var i = 0; i < dirList.length; i++) {
fileName = dirList[i];
// Any files ending in '.js' -- e.g., 'neil_pearts.js'
- if (jsPat.test(fileName)) {
+ if (JSPAT.test(fileName)) {
// Strip the '.js', e.g., 'neil_pearts'
- fileName = fileName.replace(jsPat, '');
+ fileName = fileName.replace(JSPAT, '');
// Convert underscores to camelCase with initial cap, e.g., 'NeilPearts'
ctorName = geddy.string.camelize(fileName, true);
filePath = dir + dirname + '/' + fileName;
- // Registers as a constructor, e.g., ctors.NeilPearts =
- // require('/path/to/geddy_app/<dirname>/neil_pearts').NeilPearts
- ctors[ctorName] = require(filePath)[ctorName];
+ ret.push({
+ ctorName: ctorName
+ , filePath: filePath
+ });
}
}
- for (var p in ctors) {
- ctor = ctors[p];
+ return ret;
+ }
+
+ // Load models
+ // ==================
+ , _registerModels = function (next) {
+ var dirname = '/app/models'
+ , dirList = _getDirList(dirname)
+ , item;
+ // Dynamically create controller constructors from files in app/models
+ for (var i = 0; i < dirList.length; i++) {
+ item = dirList[i];
+ require(item.filePath);
+ }
+ next();
+ }
+
+ // Load controller ctors
+ // ==================
+ , _registerConstructors = function (next) {
+ var dirname = '/app/controllers'
+ , dirList = _getDirList(dirname)
+ , item
+ , ctors = {}
+ , ctor;
+
+ // Dynamically create controller constructors from files in app/constructors
+ for (var i = 0; i < dirList.length; i++) {
+ item = dirList[i];
+ ctor = require(item.filePath)[item.ctorName];
ctor.origPrototype = ctor.prototype;
+ this.controllerRegistry[item.ctorName] = ctor;
}
- this.controllerRegistry = ctors;
next();
}
@@ -148,8 +172,9 @@ geddy.mixin(geddy, new (function () {
this.router = null;
this.FunctionRouter = FunctionRouter;
this.RegExpRouter = RegExpRouter;
- this.controllerRegistry = {};
+ this.modelRegistry = {};
this.templateRegistry = {};
+ this.controllerRegistry = {};
this.init = function () {
var self = this
@@ -162,7 +187,8 @@ geddy.mixin(geddy, new (function () {
this.config = worker.config;
items = [
- _getControllerConstructors
+ _registerModels
+ , _registerConstructors
, _loadRouter
, _loadSessionStore
, _registerTemplatePaths
View
@@ -38,7 +38,7 @@ User.prototype.someMethod = function () {
// Do some stuff on a User instance
};
-geddy.model.register('User', User);
+User = geddy.model.register('User', User);
*/
var model = {}
@@ -100,7 +100,7 @@ utils.mixin(model, new (function () {
};
- this.reg = {};
+ this.descriptionRegistry = {};
this.useTimestamps = false;
this.adapter = {};
@@ -111,7 +111,7 @@ utils.mixin(model, new (function () {
// Create the place to store the metadata about the model structure
// to use to do validations, etc. when constructing
- model.reg[name] = new model.ModelDescription(name);
+ model.descriptionRegistry[name] = new model.ModelDescription(name);
// Execute all the definition methods to create that metadata
ModelDefinition.prototype = new model.ModelDefinitionBase(name);
defined = new ModelDefinition();
@@ -134,6 +134,8 @@ utils.mixin(model, new (function () {
ModelCtor.prototype = origProto;
model[name] = ModelCtor;
+
+ return ModelCtor;
};
this.createItem = function (name, params) {
@@ -162,7 +164,7 @@ utils.mixin(model, new (function () {
};
this.validateAndUpdateFromParams = function (item, params) {
- var type = model.reg[item.type]
+ var type = model.descriptionRegistry[item.type]
, properties = type.properties
, validated = null
, errs = null
@@ -271,14 +273,14 @@ model.ModelDefinitionBase = function (name) {
this.name = name;
this.property = function (name, datatype, o) {
- model.reg[this.name].properties[name] =
+ model.descriptionRegistry[this.name].properties[name] =
new model.PropertyDescription(name, datatype, o);
};
this.validates = function (condition, name, qualifier, opts) {
var rule = utils.mixin({}, opts, true);
rule.qualifier = qualifier;
- model.reg[this.name].properties[name]
+ model.descriptionRegistry[this.name].properties[name]
.validations[condition] = rule;
};
@@ -36,5 +36,5 @@ var <%= names.constructor.singular %> = function () {
<%= names.constructor.singular %>.someStaticProperty = 'YYZ';
*/
-geddy.model.register('<%= names.constructor.singular %>', <%= names.constructor.singular %>);
+<%= names.constructor.singular %> = geddy.model.register('<%= names.constructor.singular %>', <%= names.constructor.singular %>);

0 comments on commit 4aefaa9

Please sign in to comment.