try: that.storeFoodAndWater = function(food, water)
{
var foodStore = findObjs({
name: spec.handoutName,
_type: "handout"
})[0];
if (foodStore === undefined || foodStore === null)
{
return;
}
foodStore.get("notes", function(content)
{
//var content = spec.cleanNotes(notes);
var rationJson;
if (content === "")
{
rationJson = {food: 0, water: 0};
}
else
{
rationJson = JSON.parse(content);
}
rationJson.food = rationJson.food + food;
rationJson.water = rationJson.water + water;
content = JSON.stringify(rationJson);
// defer setting until 100 milliseconds later. This might help get
// out of whatever race condition you are in...
setTimeout(function(){
foodStore.set("notes", content);
},100);
});
}; BTW, you can take advantage of some nice type coercion here and simplify your code... that.storeFoodAndWater = function(food, water) {
var foodStore = findObjs({
name: spec.handoutName,
_type: "handout"
})[0];
// undefined and null both promote to false, so using foodStore in a
// boolean context will evaluate to true if foodStore is defined.
if ( foodStore ) {
foodStore.get("notes", function(content) {
//var content = spec.cleanNotes(notes);
var rationJson;
// empty string also promotes to false, so you can do this:
rationJson = (content ? JSON.parse(content) : {food: 0, water: 0};
// javascript has shorthand operators for A = A + n, A = A - n, etc
rationJson.food += food;
rationJson.water += water;
content = JSON.stringify(rationJson);
// defer setting until 100 milliseconds later. This might help get
// out of whatever race condition you are in...
setTimeout(function(){
foodStore.set("notes", content);
},100);
});
}
};
I'll be curious to hear if that fixes the issue. Might be a bug in the way the notes' get() function is calling the callback, with it relying on some state not changing after the callback executes...