Skip to content

Instantly share code, notes, and snippets.

@ABooooo
Created April 24, 2024 09:01
Show Gist options
  • Save ABooooo/a3725ca3374aa17d4e18a945db97aadd to your computer and use it in GitHub Desktop.
Save ABooooo/a3725ca3374aa17d4e18a945db97aadd to your computer and use it in GitHub Desktop.
$('#saveNewTasklist').on("click", function () {
const nestedQuery = '.nested-sortable';
const identifier = 'taskId';
const root = document.getElementById('tasks');
function serialize(tasks) {
var serialized = [];
var children = [].slice.call(tasks.children); // children == document property
/**
* [].slice.call() == Array.prototype.slice.call()
*
* call slice() as if it was a function of NodeList using call(). What slice() does in this case is create an empty array, then iterate through the object it's running on (originally an array, now a NodeList) and keep appending the elements of that object to the empty array it created, which is eventually returned.
*/
for (var i in children) {
var nested = children[i].querySelector(nestedQuery);
serialized.push({
id: children[i].dataset[identifier],
children: nested ? serialize(nested) : []
});
}
return serialized
}
console.log(serialize(root));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment