I think this problem goes beyond typical variables and closures, or maybe I'm an idiot. It's all the same here ...
I create a bunch of objects on the fly in the jQuery plugin. The object looks something like this.
function WedgePath(canvas){ this.targetCanvas = canvas; this.label; this.logLabel = function(){ console.log(this.label) } }
The jQuery plugin looks something like this:
(function($) { $.fn.myPlugin = function() { return $(this).each(function() {
So ... the plugin creates a bunch of wedgeObjects, and then calls a global function for each of them, passing in the last instance of WedgePath. The global function looks like this.
function globalFunction(indicator_id, pWedge){ var targetWedge = pWedge; targetWedge.logLabel(); }
What happens next is that the console registers each wedge mark correctly. However, I need a little more complicated in globalFunction. So actually it looks like this ...
function globalFunction(indicator_id, pWedge){ var targetWedge = pWedge; someSql = "SELECT * FROM myTable WHERE id = ?"; dbInterface.executeSql(someSql, [indicator_id], function(transaction, result){ targetWedge.logLabel(); }) }
There is a lot going on, so I’ll explain. I use a client-side database repository (this is called WebSQL). The 'dbInterface' instance of a simple javascript object I created that handles the basics of interacting with a client database [shown at the end of this question]. The executeSql method takes up to 4 arguments
- SQL string
- optional array of arguments
- optional onSuccess handler
- optional onError handler (not used in this example)
I need this to happen: When a WebSQL query completes, it takes some of this data and manipulates some attributes of a particular wedge. But when I call logLabel on the WedgePath instance inside the onSuccess handler, I get a shortcut to the most recent WedgePath instance that was created back in the plugin code.
Now I suspect the problem is var newWedge = new WedgePath(canvas) ; line. So I tried to push each newWedge to an array, which I thought would prevent this line from replacing or overwriting the WedgePath instance at each iteration ...
wedgeArray = []; // Inside the plugin... for(var i = 1; i <= 30; i++){ var newWedge = new WedgePath(canvas); newWedge.label = "my_wedge_"+i; wedgeArray.push(newWedge); } for(var i = 0; i < wedgeArray.length; i++){ wedgeArray[i].logLabel() }
But then again, I am creating the last instance of WedgePath.
It drives me crazy. I apologize for the question, but I wanted to be as clear as possible.
END
==================================================== =============
In addition, the code for the dbInterface object should be appropriate here.
function DatabaseInterface(db){ var DB = db; this.sql = function(sql, arr, pSuccessHandler, pErrorHandler){ successHandler = (pSuccessHandler) ? pSuccessHandler : this.defaultSuccessHandler; errorHandler = (pErrorHandler) ? pErrorHandler : this.defaultErrorHandler; DB.transaction(function(tx){ if(!arr || arr.length == 0){ tx.executeSql(sql, [], successHandler, errorHandler); }else{ tx.executeSql(sql,arr, successHandler, errorHandler) } }); }