Add handlers for updating and creating when submitting a ModelForm.

This commit is contained in:
Lyudmil Angelov 2012-11-13 21:09:22 +01:00
parent 81fab293d2
commit 6a6486f698

View file

@ -59,8 +59,7 @@ var ModelForm = function (modelClass, model, formOptions) {
this.formSchema = function() {
var formSchema = {};
for (var field in model) {
if (field != '_id') {
for (var field in this.schema(model)) {
formSchema[field] = {
type: this.option(field, 'type') || model[field].constructor.name.toLowerCase(),
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');
}
}
return formSchema;
}
@ -80,39 +78,28 @@ var ModelForm = function (modelClass, model, formOptions) {
return null;
}
this.submit = function () {
for (field in model) {
if (field != '_id') {
this.submit = function (createHandler, updateHandler) {
for (field in this.schema(model)) {
var regexExpression = ':regex(id, jsonform.*' + field + ')';
var htmlElement = $(regexExpression);
if (model[field].constructor == Boolean) model[field] = !!htmlElement.attr('checked');
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) {
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) {
this.schema = function (model) {
schema = {};
for (field in model) {
if (field != '_id') schema[field] = model[field];
}
return schema;
}
}
}
var settingsForm;
@ -190,7 +177,14 @@ Template.settings.events = {
e.preventDefault();
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");
});
}
};