HTML and CSS Reference
In-Depth Information
The next step is changing the code of
populateTaskList
to make it create a list of
Task
objects from
the files found in the application's local folder. You reference the application's local folder with the
following code:
var localFolder = Windows.Storage.ApplicationData.current.localFolder;
If you want to make your application's data available for roaming, you simply reference a different
folder:
var localFolder = Windows.Storage.ApplicationData.current.roamingFolder;
Any further code that you'll be writing doesn't need updates whether you want it to save to the
machine's local or roaming folder.
To get the list of files in a folder, you use the
getFilesAsync
method. The method passes its
then
callback the list of files found in the folder. Note that if the folder contains subfolders, both subfolders
and their content are not included in the output of the
getFilesAsync
method. To get both files and
folders in the folder, you use
getItemsAsync
instead.
var localFolder = Windows.Storage.ApplicationData.current.roamingFolder;
localFolder.getFilesAsync()
.then(function (files) {
var io = Windows.Storage.FileIO;
files.forEach(function (file) {
// Do something with the file
});
});
What should you do with any retrieved file? Assuming the file contains a JSON string, you read its
content and then deserialize it to a
Task
object. Next, you add the newly created object to an array.
Finally, the array will be transformed in a binding list and displayed through the
ListView
component.
Here's the full implementation of the
TodoList.populateTaskList
method:
var tasks = new Array();
var localFolder = Windows.Storage.ApplicationData.current.roamingFolder;
localFolder.getFilesAsync()
.then(function (files) {
var io = Windows.Storage.FileIO;
files.forEach(function (file) {
io.readTextAsync(file)
.then(function (json) {
var task = TodoList.deserializeTask(json);
tasks.push(task);
})
.then(function () {
Search WWH ::
Custom Search