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>