mirror of
https://github.com/CryptKeeperZK/ejs.git
synced 2026-01-10 07:58:26 -05:00
Allow model/validations to be used client- or server-side.
This commit is contained in:
@@ -15,11 +15,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
var sys = require('sys');
|
||||
|
||||
var meta = require('geddy/lib/util/meta');
|
||||
var fleegix = require('geddy/lib/fleegix');
|
||||
|
||||
/*
|
||||
// Example model file, would be app/models/user.js:
|
||||
|
||||
@@ -42,7 +37,25 @@ exports.User = User;
|
||||
*/
|
||||
|
||||
var model = new function () {
|
||||
|
||||
var SERVER = 'server';
|
||||
var CLIENT = 'client';
|
||||
var mode;
|
||||
|
||||
if (typeof window == 'undefined') {
|
||||
mode = SERVER;
|
||||
var sys = require('sys');
|
||||
var meta = require('geddy/lib/util/meta');
|
||||
GLOBAL.fleegix = require('geddy/lib/fleegix');
|
||||
}
|
||||
else {
|
||||
mode = CLIENT;
|
||||
var GLOBAL = window;
|
||||
}
|
||||
|
||||
GLOBAL.modelRegistry = {};
|
||||
|
||||
var _constructorList = GLOBAL;
|
||||
|
||||
var _createModelItemConstructor = function (def) {
|
||||
return function () {
|
||||
@@ -53,11 +66,23 @@ var model = new function () {
|
||||
};
|
||||
|
||||
this.save = function (callback) {
|
||||
|
||||
if (this.errors) {
|
||||
callback(this.errors, null);
|
||||
if (callback) {
|
||||
callback(this.errors, null);
|
||||
}
|
||||
else {
|
||||
var err = new Error('Could not validate object. Errors: ' +
|
||||
JSON.stringify(this.errors));
|
||||
err.errors = this.errors;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
if (callback) {
|
||||
callback(null, this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -80,22 +105,26 @@ var model = new function () {
|
||||
else {
|
||||
// Introspect the list of constructors from app/models/*
|
||||
var initList = meta.registerConstructors('/app/models/', dirList);
|
||||
var modelFunc;
|
||||
for (var p in initList) {
|
||||
var modelItemDefinition = initList[p];
|
||||
// Ref to any original prototype, so we can copy stuff off it
|
||||
var origPrototype = modelItemDefinition.prototype;
|
||||
modelItemDefinition.prototype = new ValidatedModelItemCreator(p);
|
||||
var def = new modelItemDefinition();
|
||||
// Dummy constructor for instanceof check, e.g. model.User
|
||||
modelFunc = _createModelItemConstructor(def);
|
||||
modelFunc.prototype = origPrototype;
|
||||
modelFunc = fleegix.mixin(modelFunc, _createStaticMethods(p));
|
||||
GLOBAL[p] = modelFunc;
|
||||
_constructorList = initList;
|
||||
for (var p in _constructorList) {
|
||||
model.registerModel(p);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.registerModel = function (p) {
|
||||
var modelItemDefinition = _constructorList[p];
|
||||
// Ref to any original prototype, so we can copy stuff off it
|
||||
var origPrototype = modelItemDefinition.prototype;
|
||||
modelItemDefinition.prototype = new ValidatedModelItemCreator(p);
|
||||
var def = new modelItemDefinition();
|
||||
// Dummy constructor for instanceof check, e.g. model.User
|
||||
var modelFunc = _createModelItemConstructor(def);
|
||||
modelFunc.prototype = origPrototype;
|
||||
modelFunc = fleegix.mixin(modelFunc, _createStaticMethods(p));
|
||||
GLOBAL[p] = modelFunc;
|
||||
};
|
||||
|
||||
/*
|
||||
* First basic CRUD action, create
|
||||
* Next needs to pass off to desired datastore and save
|
||||
@@ -288,6 +317,32 @@ model.datatypes = {
|
||||
err: null,
|
||||
val: validated
|
||||
};
|
||||
},
|
||||
|
||||
'Object': function (name, val) {
|
||||
if (!(typeof val == 'object')) {
|
||||
return {
|
||||
err: 'Field "' + name + '" must be an Object.',
|
||||
val: null
|
||||
};
|
||||
};
|
||||
return {
|
||||
err: null,
|
||||
val: val
|
||||
};
|
||||
},
|
||||
|
||||
'Array': function (name, val) {
|
||||
if (!(val instanceof Array)) {
|
||||
return {
|
||||
err: 'Field "' + name + '" must be an Array.',
|
||||
val: null
|
||||
};
|
||||
};
|
||||
return {
|
||||
err: null,
|
||||
val: val
|
||||
};
|
||||
}
|
||||
|
||||
};
|
||||
@@ -317,7 +372,8 @@ model.validators = {
|
||||
},
|
||||
|
||||
confirmed: function (name, params, rule) {
|
||||
if (params[name] != params[rule.qualifier]) {
|
||||
var qual = rule.qualifier;
|
||||
if (params[name] != params[qual]) {
|
||||
return rule.message || 'Field "' + name + '" and field "' + qual +
|
||||
'" must match.';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user