Vulcan/client/lib/database_form/form_model.js

80 lines
2 KiB
JavaScript
Raw Normal View History

var StringUtils = {
humanize: function(string) {
return this.capitalize(this.convertCamelToSpaces(string));
},
capitalize: function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
convertCamelToSpaces: function(string) {
return string.replace(/([A-Z])/g, function(match) {
return ' ' + match;
});
}
}
var FormModel = Class.extend({
init: function(modelClass, data) {
this.modelClass = modelClass;
this.schema = this.blankSchema;
this.formOptions = {};
this.load(data)
},
load: function(data) {
if (data) {
this._id = data._id;
for (field in this.schema) {
if (data[field]) this.schema[field] = data[field];
}
}
},
formSchema: function() {
var formSchema = {};
for (var field in this.schema) {
formSchema[field] = {
type: this.option(field, 'type') || this.schema[field].constructor.name.toLowerCase(),
title: this.option(field, 'title') || StringUtils.humanize(field),
id: field,
default: this.schema[field]
}
if(this.option(field, 'enum')) formSchema[field]['enum'] = this.option(field, 'enum');
}
return formSchema;
},
option: function(field, optionName) {
if (this.formOptions[field]) return this.formOptions[field][optionName];
return null;
},
overwriteTitle: function(field, title) {
if (this.formOptions[field]) this.formOptions[field]['title'] = title;
else this.formOptions[field] = { 'title': title };
},
overwriteType: function(field, type) {
if (this.formOptions[field]) this.formOptions[field]['type'] = type;
else this.formOptions[field] = { 'type': type };
},
makeSelect: function(field, options) {
if (this.formOptions[field]) this.formOptions[field]['enum'] = options;
else this.formOptions[field] = { 'enum': options };
},
save: function(createHandler, updateHandler) {
if (this._id) {
this.modelClass.update(this._id, {$set: this.schema}, updateHandler);
} else {
this._id = this.modelClass.insert(this.schema, createHandler);
}
}
});