0

I have a working function that checks for dirty data in a form and tracks which div container the dirty element is a child of. I am trying to figure out how to store/push the {element.id:element.value} pair into an object where the keys are the div containers mapped to an array of child {element.id : element.value} pairs:

...
var dirtyData = {};
(pseudo) for each element in the form
    //isEditableData() returns the div id if we care about the div, else false
    var divContainer = isEditableData(element);
    if (!divContainer)
        continue;
    (pseudo) if the element is dirty
        cacheDirtyData(dirtyData, divContainer, element.id, element.value);
...

function cacheDirtyData(dataObj, divContainer, elementId, elementValue) {
    //I want to do something like...
    dataObj["'" + divContainer + "'"][] = {elementId : elementValue};
}

I am doing this assuming the dirtyData object is passed "by reference" and I can just add to it. I know this is not ideal code (no ctor, etc.), but I have a deadline and I'm just trying to get this pig to oink.

Chrizzis
  • 99
  • 1
  • 9

1 Answers1

0

For your "cache" function:

function cacheDirtyData (dataObj, divContainerId, elementId, elementValue) {
    if (dataObj.hasOwnProperty(divContainerId)) 
         dataObj[divContainer_ID].push({ elementId: value });
    else 
         dataObj[divContainer_ID] = [{ elementId: elementValue }];
}

You can use the Id of the div container as the string (divContainerId) you pass into the function .

Will Newton
  • 1,583
  • 13
  • 10
  • When I try to implement this, it gets caught up on the guts of the if - else. I tried a try - catch to see if I get an error it still doesn't display entire page, nor does it alert any errors. If I comment out the guts of the if - else, the page displays correctly. – Chrizzis Apr 05 '14 at 18:33
  • Tinkering with it, I got it to work. Here is what I had to do to get it to wori: – Chrizzis Apr 05 '14 at 18:58
  • 1
    Sorry, spoke too soon. I was troubleshooting my dev environment and refreshed the scripts on the browser and now it works. Thanks! – Chrizzis Apr 05 '14 at 19:10
  • Found further error while I was displaying the data, then found this answer here: [http://stackoverflow.com/questions/2873163/is-this-javascript-object-literal-key-restriction-strictly-due-to-parsing](http://stackoverflow.com/questions/2873163/is-this-javascript-object-literal-key-restriction-strictly-due-to-parsing). To get it to properly work, I had to create the element object (`var dirtyElement = {}; dirtyElement[elementId] = elementValue;`), then use it with the dataObj (`dataObj[divContainer].push(dirtyElement)`). – Chrizzis Apr 05 '14 at 20:16