Lets say that I've the following structure:
<textarea id="content"></textarea>
<select id="sel">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
</select>
The HTML elements are dynamically rendered from the js and the associated javascript object with this elements is as follow:
var objectData = {
"textareacontent" : "some random content"
"selectData" : "1"
}
Now lets say that i'm getting this object dynamically and then I need to post this data back to the server, after the user make changes to the HTML elements (the textarea and the select).
So the user changes the textarea content and the choose different option from the previous selected option 1. What I'm doing now is just take all the data again from the DOM and send it to the server.
As my code keep growing I'm noticing that this approach is very bad and I'm starting to think about the approach that my js object(objectData) needs to be in synchronized mode with the HTML elements, so whenever the user changes the textarea content, the objectData.textareacontent will dynamically change with the user interaction and then instead of collecting everything from the DOM (Because if I have lets say 30 or 100 selects with 50 options each, collecting the value from them will be nightmarish. And the DOM is SLOW). And then just send the objectData to the server.
I'm looking for the best practices here and a simple example will be greatly appreciated. Also I've noticed the observer pattern
Object.observe
which seems good, but looks like it is only for when changing the js object state and not keeping async the object state and the associated HTML Element (or I'm missing something)