Skip to content

Instantly share code, notes, and snippets.

@amogower
Created March 9, 2016 12:40
Show Gist options
  • Select an option

  • Save amogower/fd162963db6cefa4a4cb to your computer and use it in GitHub Desktop.

Select an option

Save amogower/fd162963db6cefa4a4cb to your computer and use it in GitHub Desktop.
Check form for changes and only get the difference
function getFormOriginalState()
{
startItems = convertSerializedArrayToHash($form.serializeArray());
}
function checkFormForChanges()
{
var currentItems = convertSerializedArrayToHash($form.serializeArray());
var dataToSubmit = hashDiff(startItems, currentItems);
return dataToSubmit;
}
function hashDiff(hash1, hash2) {
var diff = {};
for (var k in hash2)
{
if (hash1[k] !== hash2[k])
{
diff[k] = hash2[k];
}
}
return diff;
}
function convertSerializedArrayToHash(array) {
var r = {};
for (var i = 0; i<array.length; i++)
{
r[array[i].name] = array[i].value;
}
return r;
}
function getFormData()
{
var data = checkFormForChanges();
data.user_id = user.id;
validateFormData(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment