I'm working on offloading some url polling requests to a web worker. To do this, I need to fetch certain attributes from the DOM element of invokation, pass them into a url request, then update the original DOM element with the results. Since multiple DOM elements use this function to fetch updates, I need to pass through $(this)
or an equivalent unique identifier to ensure the correct element is updated.
I understand from questions such as "Passing objects to a web worker" and "Can I pass a jQuery object to a web worker" that this is not possible, so I am looking for a means to emulate this.
Here is a rough outline of my code:
//main.js
function update(){
var data = { 'id' : $(this).attr('itemid'),
'filter' : $(this).attr('filter')}
updatePoller.postMessage(data);
}
//worker.js
this.onmessage = function(e){
//format params from e.data
//make GET request to url
//when done...
postMessage(req.responseText);
}
//main.js (again)
updatePoller.onmessage = function(message){
//process response
//update child elements of $(this)
}
As you can see, I don't need to access $(this)
inside the web worker, but I need the reference once the request has returned in order to update the correct element. Are there any ways to pass a unique reference to a DOM element through a web worker?