HTML and CSS Reference
In-Depth Information
Adding a data serialization component
Some changes are required to the TodoList.invokeSavePicker method to make it save the current
Task object. First, you change the line that calls into writeTextAsync so that instead of writing down a
constant sample text, it now writes the string returned by a new wrapper function:
.writeTextAsync(file, TodoList.serializeTask(task))
In addition, you write the skeleton of the TodoList.serializeTask function and add it to todolist.js :
// Task Serializer
TodoList.serializeTask = function (task) {
// Just save the description of the task
return task.description;
In this way, the file saves only the description of the task but loses all other information. How
would you save the entire data set of a Task object?
A quick answer to the question simply says that the serialization format is entirely up to you. You
should just be aware that the TodoList.serializeTask function will return a string that is saved as is to
the file. From the perspective of the function, whatever string works well.
The Task object is made of several pieces of information: description, due date, priority, and so
forth. You might want to save them all. One option could be concatenating each piece of information
using a particular character (such as, the pipe character | ) as the separator. Here's an example:
TodoList.serializeTask = function (task) {
return task.description + "|" +
task.dueDate + "|" +
task.priority + "|" +
task.status + "|" +
The resulting string may look like the one shown below:
This is my new task|Sat Dec 1 12:00:00 UTC+0100 2012|3|Not Started|0
There's nothing wrong with this format, except that it requires an ad hoc piece of code to be read
and parsed back to a Task object. The process of transforming a string of text to an object is referred
to as deserialization .
Search WWH ::

Custom Search