Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[sheet worker] please check a little js

1457118604

Edited 1457118627
vÍnce
Pro
Sheet Author
Warning: JS Newbie Alert. Assume nothing. I'm working on adding a new attribute ( @{HPCurrent_max} ) to a sheet that checks for the value of the old attribute ( @{HP|max} ), substitutes the value into the new attribute if present, and after this is done detects changes on the new attribute to update the old attribute for backward compatibility.   After much hair pulling and teeth grinding, and with the help of Chris's sheet version function, I think things are working as expected.  But, I would love for more experienced eyes to have a look at the code, specifically the bolded text and see if I should make any changes.  Again, apologies if there are some blatant js faux pas. TIA < script type = "text/worker" > var Sheet = Sheet || (function() {   var version = 0.1,     Debug = false,     updateHP = function() {       getAttrs(["HP|max", "HPCurrent_max"], function(values) {         var HPmax = (parseInt(values["HP|max"]));         var HPCurrent_max = (parseInt(values["HPCurrent_max"]));         if ((parseInt(values["HP|max"])) === 0) {           console.log("....No conversion necessary. HP|max = " + (parseInt(values["HP|max"])));           return (values);         } else {           setAttrs({             "HPCurrent_max": HPmax           });           getAttrs(["HPCurrent_max"], function(values) {             var HPCurrent_max = (parseInt(values["HPCurrent_max"]));             console.log("....HPCurrent_max now equals HP|max = " + HPCurrent_max);             setAttrs({               "HP|max": HPCurrent_max             });             console.log("....HPCurrent_max = " + HPCurrent_max);             console.log("...........HP|max = " + (parseInt(values["HP|max"])));           });         }       });     },     /* recalculateSheet */     recalculateSheet = function(oldversion) {       if (oldversion < 0.1) {         updateHP();       }     },     /* checkForUpdate looks at current version of page in Sheet_Version and compares to code Sheet.version  calls recalulateSheet if versions don't match or if recalculate button was pressed.  */     checkForUpdate = function() {       getAttrs(["Sheet_Version", "Sheet_forcesync", "recalc1"], function(v) {         var setter = {}, currVer = 0,           setAny = 0,           recalc = false;         currVer = parseFloat(v["Sheet_Version"], 10) || 0;         console.log("Current sheet data version:" + currVer + ", Sheet code version:" + version);         if (currVer !== version) {           recalc = true;           setter["Sheet_Version"] = version;           setAny = 1;         }         if (v["recalc1"] && v["recalc1"] != "0") {           currVer = -1;           recalc = true;           setter["recalc1"] = 0;           setAny = 1;         }         if (v["Sheet_forcesync"] && v["Sheet_forcesync"] != "0") {           currVer = -1;           recalc = true;           setter["Sheet_forcesync"] = 0;           setAny = 1;         }         if (setAny) {           setAttrs(setter);         }         if (recalc) {           recalculateSheet(currVer);         }       });     };   return {     version: version,     checkForUpdate: checkForUpdate,     updateHP: updateHP   }; }()); //Sync HP|max with HPCurrent_max on("change:hpcurrent_max", function() {   getAttrs(["HPCurrent_max", "HP|max"], function(values) {     var HPCurrent_max = (parseInt(values["HPCurrent_max"]));     setAttrs({       "HP|max": HPCurrent_max     });     getAttrs(["HPCurrent_max", "HP|max"], function(values) {       console.log("....HPCurrent_max = " + HPCurrent_max);       console.log("...........HP|max = " + (parseInt(values["HP|max"])));     });   }); }); //sheet on("sheet:opened", function() {   Sheet.checkForUpdate(); }); on("change:recalc1", function() {   Sheet.checkForUpdate(); }); < /script>
1457121969
The Aaron
Pro
API Scripter
It's considered good practice (by people other than Brian!) to always include a radix with your parseInt() calls. parseInt(values["HPCurrent_max"] ,10 ); setAttrs() isn't guaranteed to immediately have changed the values, so to get them after a set, you'll need to use the callback: setAttrs({     "HPCurrent_max": HPmax } ,{},function(){     getAttrs(["HPCurrent_max"], function(values) {         var HPCurrent_max = (parseInt(values["HPCurrent_max"],10));         console.log("....HPCurrent_max now equals HP|max = " + HPCurrent_max);         setAttrs({             "HP|max": HPCurrent_max         });         console.log("....HPCurrent_max = " + HPCurrent_max);         console.log("...........HP|max = " + (parseInt(values["HP|max"],10)));     }); } ); /* ... */ setAttrs({     "HP|max": HPCurrent_max } ,{},function(){     getAttrs(["HPCurrent_max", "HP|max"], function(values) {         console.log("....HPCurrent_max = " + HPCurrent_max);         console.log("...........HP|max = " + (parseInt(values["HP|max"],10)));     }); } );  Not sure about the functionality, but that's all that stuck out to me!  =D
1457122116
The Aaron
Pro
API Scripter
Oh, just on a point of style, I'd probably break that really long comment across lines:     /* checkForUpdate looks at current version of page in      * Sheet_Version and compares to code Sheet.version calls      * recalulateSheet if versions don't match or if recalculate      * button was pressed.  */ ( And while you're at it, you might as well read Javascript: The Good Parts by Douglas Crockford !  =D  (You knew it was coming!) )
1457123726

Edited 1457123747
vÍnce
Pro
Sheet Author
LOL I knew you were going to say that.  ;-)   It's on my amaz0n wishlist.  Then it can sit next to my dusty Javascript for Web Developers book... Thanks Aaron.