Jquery store sql result callback in var

Jquery store sql result callback in var

Hi Guys,

i have a little problem getting to the results of a sql count function.

this is what i am trying to do:

table 1: product categories
    cid = uniek
    category_name = TEXT

table 2: products
   pid = uniek
   cid = link to category
   type = subcategorie (there are only 2 types)
   product_name = TEXT

I want a listview of all categories_name en the number of products of type 1 and type 2 that are in that categorie in to seperate <span class="ui-li-count">.

The sql and loop for the category listview is working fine, now i want to add a function to this loop that counts the number of type 1 en type 2 product in each category.

My problem is storing the result of the count function in a way that i can use it in the creation of the listview.

Code: Create listview loop
  1. this.store = new select_functions()
  2. this.store.select_categorie(function(result_array){
  3.       var l = result_array.length;
  4.       var e;               
  5.      
  6.       for (var i=0; i<l; i++) {
  7.             e = result_array[i];
  8.      
  9.             this.count = new count_functions();
  10.             var type1 =  this.count.count_products_by_type(e.cid, "type1", callback); //not working
  11.             var type2 = this.count.count_products_by_type(e.cid, "type2", callback); //not working

  12.             $('#categorie_listview').append('<li"><a href="#">'+ e.categorie_name+'</a><span class="ui-li-count">'+type1+'</span><span class="ui-li-count">'+type2+'</span></li>');
  13.         }
  14. });
SQL count function: (seperate file with collection of different count functions)
  1. var count_functions = function(successCallback, errorCallback) {
  2.     this.initializeDatabase = function(successCallback, errorCallback) {
  3.         var self = this;       
  4.         this.db = window.openDatabase("product_DB", "1.0", "Product db", 200000);     
  5.     }   
  6.     this.count_products_by_type = function(cid, type, callback) {
  7.        
  8.         this.db.transaction(
  9.             function(tx) {
  10.                            
  11.                 var sql = "SELECT count(pid) as count FROM products WHERE product.cid = ? AND type = ?";                                     
  12.                                
  13.                 tx.executeSql(sql, [cid, type], function(tx, results) {
  14.                     var count = results.rows.item(0);                   
  15.                     callback(count);
  16.                 });
  17.             },        
  18.             function(error) {
  19.                 alert("Transaction Error: " + error.message);
  20.             }           
  21.         );
  22.     } 
  23.       this.initializeDatabase(successCallback, errorCallback);
  24. }
As de the result of this query will alway be a singel value there is no loop needed, and i want to store the callback of this function in the type 1 en type 2 vars i create in the categorie loop.

My question:
How can i store the callback of the sql result in a variable so i can use it in the categorie loop.


if someone could point me in the right direction, any help would be much obliged.

thanks in advance