javascript - How to update a document in Meteor with multiple input fields and a single update button -


i've got modal pops , lets user update fields in collection. example, have multiple input fields client's address (the modal has single button perform update):

... <input type="text" id="full-name-change-modal"> <input type="text" id="address-change-modal"> <input type="text" id="city-change-modal"> <input type="text" id="state-change-modal"> ... 

so if user wants change full name of client, update include fields appropriate input wasn't empty. in case update read like:

collection.update({ _id: documentnameaddress._id}, {$set: {fullname: <name input> }}); 

if user wanted change address, city, state , zip, it'd need read:

demographic.update({ _id: documentnameaddress._id}, {$set: {address: <address input>, city: <city input>, state: <state input>, zip: <zip input> }); 

i'd rather not have separate button every field if possible. how this?

you'll end 1 optional update collection , can build update demographic based on non-empty fields in form:

template.mytemplate.events({   'click button':function(ev){      var fullname = $("#full-name-change-modal").val();     if ( fullname ) collection.update({ _id: documentnameaddress._id}, {$set: {fullname: fullname }});      var query = {};     var address = $('#address-change-modal').val();     if ( address ) query.address = address;     var city = $('#city-change-modal').val();     if ( city) query.city = city;     var state = $('#state-change-modal').val();     if ( state ) query.state = state;     if ( object.keys(query).length ) demographic.update(query);   } }); 

Comments