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


Can you use custom reset method of DOM node form?

"submit form":function(event,template){
  event.preventDefault();
  // ...
  template.find("form").reset();
}

      



http://www.w3schools.com/jsref/met_form_reset.asp

+13


source


Access the DOM object that triggered the event and reset via event.target.reset ();

"submit form":function(event){
   event.preventDefault();
   //...
   event.target.reset();
}

      



http://docs.meteor.com/#/full/eventmaps

+4


source







All Articles