dimanche 28 juin 2020

Migrating from localstorage to localforage - saving a checkbox

I have some localstorage code to save whether a checkbox is checked or not to hide or show a div in a group of div's (as well as hide it for printing if checked). It works well. Trying to migrate the same code to localforage fails. Most likely due to my own lack of understanding! I am hoping someone can point me in the right direction.

My localstorage code:

var checkboxValuesCommon=JSON.parse(localStorage.getItem("formValuesCommon"))||{},
$checkboxesCommon=$("input.printitCommon"),
$commonDrugCommon=$(".eachdrug.eachcommondrug");

function updateStorageCommon(){
$checkboxesCommon.each(function()
{
checkboxValuesCommon[this.id]=this.checked,this.checked? 
$(this).closest(".eachdrug.eachcommondrug").removeClass("hidden-print"):$(this).closest(".eachdrug.eachcommondrug").addClass("hidden-print")}),
localStorage.setItem("formValuesCommon",JSON.stringify(checkboxValuesCommon))}

$checkboxesCommon.on("change",function(){
this.checked?$(this).closest(".eachdrug.eachcommondrug").removeClass("hidden-print"):$(this).closest(".eachdrug.eachcommondrug").addClass("hidden-print"),updateStorageCommon()}),

$.each(checkboxValuesCommon,function(e,r) 
{$("#"+e).prop("checked",r)}),
$checkboxesCommon.each(function(){
$(this).is(":checked")?$(this).closest(".eachdrug.eachcommondrug").removeClass("hidden-print hidden"):$(this).closest(".eachdrug.eachcommondrug").addClass("hidden-print hidden")
});

Localforage code:

localforage.getItem('formValuesCommon').then(function(value) {
var checkboxValuesCommon = value;
var $checkboxesCommon = $("input.printitCommon"),
$commonDrugCommon = $(".eachdrug.eachcommondrug");
function updateStorageCommon() {


$checkboxesCommon.each(function() {
    checkboxValuesCommon[this.id] = this.checked, this.checked ?  $(this).closest(".eachdrug.eachcommondrug").removeClass("hidden-print") : $(this).closest(".eachdrug.eachcommondrug").addClass("hidden-print")
}); localforage.setItem("formValuesCommon", true);
}

 $checkboxesCommon.on("change", function() {
this.checked ? $(this).closest(".eachdrug.eachcommondrug").removeClass("hidden-print") :  $(this).closest(".eachdrug.eachcommondrug").addClass("hidden-print"); 
updateStorageCommon()
}); 
$.each(checkboxValuesCommon, function(e, r) {
$("#" + e).prop("checked", r)
}); 
$checkboxesCommon.each(function() {
this.checked ? $(this).closest(".eachdrug.eachcommondrug").removeClass("hidden-print hidden") : $(this).closest(".eachdrug.eachcommondrug").addClass("hidden-print hidden")
});
});



Aucun commentaire:

Enregistrer un commentaire