Meteor: the most "meteor" way to clear form fields
I'm working on a sample CRUD app with Meteor.js and don't know the best way to clear form fields. I need this in two places: when I click Submit and I click Cancel.
I implemented it this way by creating a utility function called clearFormFields () that just uses jQuery to empty their contents, but doesn't feel "meteoric" as it should; I believe this should be better, so it doesn't have global visibility. What am I doing wrong?
function clearFormFields() {
$("#description").val("");
$("#priority").val("");
}
Template.todoNew.events({
'click #cancel': function(event) {
event.preventDefault();
Session.set('editing', false);
clearFormFields();
},
'submit form': function(event) {
event.preventDefault();
var theDocument = {
description: event.target.description.value,
priority: event.target.priority.value
};
if (Session.get("editing")) {
Meteor.call("updateTodo", theDocument, Session.get('theDocumentId'))
}
else {
Meteor.call("insertTodo", theDocument);
}
Session.set('editing', false);
clearFormFields();
/* Could do this twice but hate the code duplication.
description: event.target.description.value = "";
priority: event.target.priority.value = "";
*/
}
});
+3
source to share
2 answers