I have created code so that whenever the user goes to edit the servicename input(hair in the example) the word update gets pushed into the originals array.lines 1(array),6(method to push).
The problem is that if the user adds/removes 3(for example) chars the array will be filled with the identical value many times(3, for the example).
How can I avoid that?
regardless of the chars added or removed “update” must be pushed only once in the array.
[quote=“designtrooper, post:3, topic:296920, full:true”]Sorry…but I do not understand what are you talking about.
Originals is the array and I add to it the string “update”.
[/quote]
Are you wanting to keep a record of what the input field originally contained?
If the goal is to just have update only get set once, no matter how many actual changes have occurred, set update as a flag instead.
var update = false;
on (do all the things) {
update = true; //Doesnt matter if this runs multiple times.
You dont appear to care about the -location- of update in the array, dont do anything with the array, and only want it once. So… what’s the point of putting it in the array? Just leave it as a boolean and act on it accordingly when you evaluate the array for a purpose.
I guess the most reliable way to do so would still be checking for their .defaultValues as suggested by @Paul_Wilkins, so you don’t have to manually maintain such an array (including taking care of duplicates etc.)… like
var getUpdatedInputs = function () {
var inputs = document.querySelectorAll('input')
return Array.from(inputs).filter(function (input) {
return input.value !== input.defaultValue
})
}
You might then also .map() the returned array to "updated" strings, if this is what you absolutely want… although I’m wondering in what way such a list of equal strings could serve any useful purpose.
In repyling…I am afraid I will be lengthy…but it is necessary though.
You said to set a flag…(more or less) that was the scheme I had before considerting the array. More precisely I had an object property and set it’s value accordingly:
originals.status='update';
But there is a problem with the above…look at this fiddle
When cliciking the user has the option of adding a service(click add) or updating/editing on of these who already exist.
Depending what is done a flag is set(in the corresponding handlers) line 180 and line 39.
As you see though the value is assigned to the same object property(probably wrong)…now… this info must be sent to the server-so that different functions get called.
There is a scenario though where the user may add AND update a service…hence I chose to store the values in an array.
After having read the above what is your suggestion?
First impulse: Every change is a change, whether its an update or an add. The -server side- is the one to catch that and fix it. Keep in mind that many backend storage systems have mechanisms for this (INSERT…ON DUPLICATE KEY UPDATE, for MySQL for example).
As for how I’d handle it, as been stated previously, you dont care about the state of the system until you need to commit your changes. At that time, compare your form’s current state to its original state, and submit changes as necessary.
Rather than catching a change when it happens, evaluate all changes at submission time.
For example: If I change a service name from “Hair” to “Haircut”, you register a change. If I then change it back to “Hair” before saving… you register another change. But I haven’t actually changed anything that your system needs to update, because it already had the information in place.
If you’re wanting to make a copy so that you can return the form values back to what they started at when someone cancels editing, an easier way is to just make a copy of the form.
var form = ...
var formBackup = Object.create(form);
...
if (cancelEdit) {
form.replaceWith(formBackup);
}
The current state of the form can be retrieved at the same time by reading the currentvalue of each form row’s elements…
What you may want to do is forEach across the set of wrapper_servp classed DOM objects ($(".wrapper_servp") ), especially since you’re going to be looking into <span> tags for some of the data (Incidentally, I’d recommend some hidden input elements to make that data more accessible.)
Your goal then, becomes essentially to be able to construct 2 arrays of objects, the original state and the current one, and find the difference between the two.
Filter the Current array such that it contains only elements that do not match an item in the Original array.
Submit that filtered array to the backend. (Alternatively, just send the whole current array to the backend and sort it out there, but that’s more of a system-intensive process, because the backend does not inherently know the original state of the form.)
That doesn’t seem to be relevant though, for each time the Edit button is used, the current state at that time before editing needs to be saved instead for when someone chooses to cancel the current edit.