mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 18:11:40 -05:00
Add handlers for updating and creating when submitting a ModelForm.
This commit is contained in:
parent
81fab293d2
commit
6a6486f698
1 changed files with 32 additions and 38 deletions
|
@ -59,8 +59,7 @@ var ModelForm = function (modelClass, model, formOptions) {
|
||||||
this.formSchema = function() {
|
this.formSchema = function() {
|
||||||
var formSchema = {};
|
var formSchema = {};
|
||||||
|
|
||||||
for (var field in model) {
|
for (var field in this.schema(model)) {
|
||||||
if (field != '_id') {
|
|
||||||
formSchema[field] = {
|
formSchema[field] = {
|
||||||
type: this.option(field, 'type') || model[field].constructor.name.toLowerCase(),
|
type: this.option(field, 'type') || model[field].constructor.name.toLowerCase(),
|
||||||
title: this.option(field, 'title') || StringUtils.humanize(field),
|
title: this.option(field, 'title') || StringUtils.humanize(field),
|
||||||
|
@ -70,7 +69,6 @@ var ModelForm = function (modelClass, model, formOptions) {
|
||||||
|
|
||||||
if(this.option(field, 'enum')) formSchema[field]['enum'] = this.option(field, 'enum');
|
if(this.option(field, 'enum')) formSchema[field]['enum'] = this.option(field, 'enum');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return formSchema;
|
return formSchema;
|
||||||
}
|
}
|
||||||
|
@ -80,39 +78,28 @@ var ModelForm = function (modelClass, model, formOptions) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.submit = function () {
|
this.submit = function (createHandler, updateHandler) {
|
||||||
for (field in model) {
|
for (field in this.schema(model)) {
|
||||||
if (field != '_id') {
|
|
||||||
var regexExpression = ':regex(id, jsonform.*' + field + ')';
|
var regexExpression = ':regex(id, jsonform.*' + field + ')';
|
||||||
var htmlElement = $(regexExpression);
|
var htmlElement = $(regexExpression);
|
||||||
if (model[field].constructor == Boolean) model[field] = !!htmlElement.attr('checked');
|
if (model[field].constructor == Boolean) model[field] = !!htmlElement.attr('checked');
|
||||||
else model[field] = htmlElement.val();
|
else model[field] = htmlElement.val();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (model._id) {
|
||||||
|
modelClass.update(model._id, {$set: this.schema(model)}, updateHandler);
|
||||||
|
} else {
|
||||||
|
model._id = modelClass.insert(this.schema(model), createHandler);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(model._id) {
|
this.schema = function (model) {
|
||||||
modelClass.update(model._id,{
|
|
||||||
$set: schema(model)
|
|
||||||
}, function(error){
|
|
||||||
if(error)
|
|
||||||
console.log(error);
|
|
||||||
throwError("Settings have been updated");
|
|
||||||
});
|
|
||||||
}else{
|
|
||||||
var settingId = modelClass.insert(schema(model), function(){
|
|
||||||
throwError("Settings have been created");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function schema(model) {
|
|
||||||
schema = {};
|
schema = {};
|
||||||
for (field in model) {
|
for (field in model) {
|
||||||
if (field != '_id') schema[field] = model[field];
|
if (field != '_id') schema[field] = model[field];
|
||||||
}
|
}
|
||||||
return schema;
|
return schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var settingsForm;
|
var settingsForm;
|
||||||
|
@ -190,7 +177,14 @@ Template.settings.events = {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if(!Meteor.user()) throw 'You must be logged in.';
|
if(!Meteor.user()) throw 'You must be logged in.';
|
||||||
|
|
||||||
settingsForm.submit();
|
settingsForm.submit(
|
||||||
|
function(){
|
||||||
|
throwError("Settings have been created");
|
||||||
|
},
|
||||||
|
function(error) {
|
||||||
|
if(error) console.log(error);
|
||||||
|
throwError("Settings have been updated");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue