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
- this.store = new select_functions()
- this.store.select_categorie(function(result_array){
- var l = result_array.length;
- var e;
-
- for (var i=0; i<l; i++) {
- e = result_array[i];
-
- this.count = new count_functions();
- var type1 = this.count.count_products_by_type(e.cid, "type1", callback); //not working
- var type2 = this.count.count_products_by_type(e.cid, "type2", callback); //not working
- $('#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>');
- }
- });
SQL count function: (seperate file with collection of different count functions)
- var count_functions = function(successCallback, errorCallback) {
- this.initializeDatabase = function(successCallback, errorCallback) {
- var self = this;
- this.db = window.openDatabase("product_DB", "1.0", "Product db", 200000);
- }
- this.count_products_by_type = function(cid, type, callback) {
-
- this.db.transaction(
- function(tx) {
-
- var sql = "SELECT count(pid) as count FROM products WHERE product.cid = ? AND type = ?";
-
- tx.executeSql(sql, [cid, type], function(tx, results) {
- var count = results.rows.item(0);
- callback(count);
- });
- },
- function(error) {
- alert("Transaction Error: " + error.message);
- }
- );
- }
- this.initializeDatabase(successCallback, errorCallback);
- }
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