HTML and CSS Reference
In-Depth Information
Unfortunately we immediately have a problem: there are two references in this code to the
jQuery function “$”. This object is registered on the window object, and therefore is not
accessible from the Web Worker.
The first of these (the $.each) can easily be
replaced with a for loop:
for (var indx = 0; indx < lines.length; indx++) {
var val = lines[indx];
if (indx >= 1 && val) {
var task = loadTask(val);
if (task) {
tasks.push(task);
}
}
};
Unfortunately we are stuck when it comes to the use of the csv function:
$.csv.toArray(csvTask);
It is not even possible to pass the jQuery object into the Web Worker. Ultimately we would
have no choice but to find a different library for parsing CSV files.
Although this would not be a problem, it is not the subject of this chapter, so we will simply
replace that line with the simplified code for splitting the line each time a comma is en-
countered. As discussed above, there are many real world issues with this code:
function loadTask(csvTask) {
var tokens = csvTask.split(',');
if (tokens.length == 3) {
var task = {};
task.task = tokens[0];
task.requiredBy = tokens[1];
task.category = tokens[2];
return task;
}
return null;
}
The JavaScript file containing the Web Worker does not need to be explicitly imported in
tasks.html. Because we want the worker to be available offline however, we do need to add
Search WWH ::




Custom Search