samedi 26 décembre 2015

How to select all checkboxes in Meteor form, then save value to Database

I've got a form that allows users to select some options which are then saved to the database. There are a lot of options and in most cases users will want to select them all.

Currently, I have it set up so that when each checkbox is clicked either on or off, it automatically updates the database boolean BUT I don't think this is the best way to do this. It also means if I put in some jQuery to select all checkboxes then the database values don't update.

So what I'm looking for is a way to make a checkbox or button that selects all checkboxes in a form, then a submit button that gets the value of all the checkboxes and saves them in to the database. The information is not sensitive so it can be saved to the user profile

My checkboxes are just normal checkboxes with unique id's in a form

Here is my JS (I've commented each block so you see what it does

if (Meteor.isClient) {

  //this block sets each alert value in the db to false as default
  Template.chooseAlerts.onCreated(function () {
    Meteor.users.update({ _id: Meteor.userId() }, {$set: { 'profile.chargeSucceeded': false, 'profile.chargeFailed': false, 'profile.chargeRefunded': false, 'profile.customerCreated': false, 'profile.customerDeleted': false, 'profile.subscriptionCreated': false,'profile.subscriptionUpdated': false,'profile.subscriptionDeleted': false, 'profile.trialEnding': false}});
  });

  //this is the logic for submitting the form, it flashes a message to the user and moves to the next page (this is where I'll need to store the checkbox values to the db
  Template.chooseAlerts.events({
    'submit #chooseAlerts': function(e) {
      e.preventDefault();
      var submitButton = $('button[type="submit"]').button('loading');
      sAlert.success('Great, we\'ve saved those alerts!', {onRouteClose: false, timeout: 2500});
      Router.go('subscribe');
      submitButton.button('reset');   
    },

    //the following blocks are just events for changing the db value when a checkbox is clicked. There are more of these but they are all the same so I have excluded them
    'click #chargeSucceeded': function(evt) {
      var value = evt.target.checked;
      Meteor.users.update({ _id: Meteor.userId() }, {$set: { 'profile.chargeSucceeded': value}});
    },
    'click #chargeFailed': function(evt) {
      var value = evt.target.checked;
      Meteor.users.update({ _id: Meteor.userId() }, {$set: { 'profile.chargeFailed': value}});
    },
    'click #chargeRefunded': function(evt) {
      var value = evt.target.checked;
      Meteor.users.update({ _id: Meteor.userId() }, {$set: { 'profile.chargeRefunded': value}});
    },
    'click #customerCreated': function(evt) {
      var value = evt.target.checked;
      Meteor.users.update({ _id: Meteor.userId() }, {$set: { 'profile.customerCreated': value}});
    },
    'click #customerDeleted': function(evt) {
      var value = evt.target.checked;
      Meteor.users.update({ _id: Meteor.userId() }, {$set: { 'profile.customerDeleted': value}});
    }
  });




Aucun commentaire:

Enregistrer un commentaire