Results 1 to 3 of 3
  1.    #1  
    How do I avoid nesting a lot of functions together. Here's what I mean:

    Code:
    try {
        this.db = openDatabase(database_name, database_version, "Data DB " + database_version, estimated_database_size);
    } 
    catch (e) {
        Mojo.Log.info("error creating/opening DB " + database_name + " version " + database_version + " ERROR: " + e);
    }
    
    var dataAjaxURL = 'http://example.com';
    
    var dataAjaxAttributes = {
        method: 'post',
        evalJSON: 'true', //use JSON type of data handling from the web page
        parameters: dataAjaxParameters,
        onSuccess: function receivesuccess(response){
        
            // go through every value in results and store in database
            response = response.responseText.evalJSON();
            
            for (var j = response.length - 1; j >= 0; j = j - 1) { //go through each result
                zip = response[j].zip;
                //check to see if this is a duplicate of something we already have in the database
                sql = 'SELECT * FROM ' + data_table_name + ' WHERE zip = "' + zip + '"; ';
                if (this.db) {
                    try {
                        this.db.transaction((function(transaction){
                            transaction.executeSql(sql, [], function compare_current_with_new(transaction, results){
                                if (results.rows.length == 0) { //not a duplicate entry of somethind already in our DB
                                    //create sql statement for inserting into the database
                                    string[string.length + 1] = 'INSERT INTO ' + data_table_name + ' (zip)' +
                                    ' VALUES ("' +
                                    zip +
                                    '"); ';
                                }
                            }, function error_inserting(transaction, error){
                                Mojo.Log.info("update-data: error  '" + error.message + "' (Code: " + error.code + ")");
                            });
                        }).bind(this));
                    } 
                    catch (e) {
                        Mojo.Log.info("ERROR selecting from data table: " + e);
                    }
                }
                else 
                    Mojo.Log.info("Bad DB Connection");
            }
        }
    .bind(this)    ,
        onFailure: function error_receiving(response){
            Mojo.Log.info("update-data: error downloading cache data page")
        }
    };
    var request = new Ajax.Request(dataAjaxURL, dataAjaxAttributes);
    As you can see, I have a nest of functions 3 deep. This makes it really hard to follow my code. How do I get around this? I've been reading about callbacks, is that something I should be looking into?
  2. #2  
    Here's an example where I pulled out the receivesuccess and error_inserting functions from within the dataAjaxAttributes object. The two db callback functions have closures on sql and zip variables so those are easier to keep inside the receivesuccess function, but they can still be declared as named variables instead of anonymous functions.

    Code:
    var error_inserting = function (transaction, error){
        Mojo.Log.info("update-data: error  '" + error.message + "' (Code: " + error.code + ")");
    };
    
    var receivesuccess = function(response){
        var sql, zip;
        var transaction_func = function(transaction){
            transaction.executeSql(sql, [], compare_current_with_new, error_inserting);
        };
        var compare_current_with_new = function(transaction, results){
            if (results.rows.length == 0) { //not a duplicate entry of somethind already in our DB
                //create sql statement for inserting into the database
                string[string.length + 1] = 'INSERT INTO ' + data_table_name + ' (zip)' +
                ' VALUES ("' +
                zip +
                '"); ';
            }
        };
    
        // go through every value in results and store in database
        response = response.responseText.evalJSON();
        
        for (var j = response.length - 1; j >= 0; j = j - 1) { //go through each result
            zip = response[j].zip;
            //check to see if this is a duplicate of something we already have in the database
            sql = 'SELECT * FROM ' + data_table_name + ' WHERE zip = "' + zip + '"; ';
            if (this.db) {
                try {
                    this.db.transaction(transaction_func);
                } 
                catch (e) {
                    Mojo.Log.info("ERROR selecting from data table: " + e);
                }
            }
            else 
                Mojo.Log.info("Bad DB Connection");
        }
    }.bind(this);
    
    var dataAjaxAttributes = {
        method: 'post',
        evalJSON: 'true', //use JSON type of data handling from the web page
        parameters: dataAjaxParameters,
        onSuccess: receivesuccess,
        onFailure: function error_receiving(response){
            Mojo.Log.info("update-data: error downloading cache data page")
        }
    };
  3. #3  
    Quote Originally Posted by Jared5755 View Post
    As you can see, I have a nest of functions 3 deep. This makes it really hard to follow my code. How do I get around this? I've been reading about callbacks, is that something I should be looking into?
    The way I make my code easier to follow is to have one function call one or more other functions. It also definitely helps to add a lot of comments and make use of Mojo.Log.info

    An example in my code:
    Code:
    HomeAssistant.prototype.successDB = function(transaction, results) {
    // RETRIEVE VALUES FROM DATABASE TRANSACTION
            Mojo.Log.info("HOME SCENE successDB has been called");
            this.myI = results.rows.item(0).myindex
            this.myP = results.rows.item(0).mypath;
    // IF AN INDEX IS FOUND, THEN
    	if(this.myI) {
                    Mojo.Log.info("HOME SCENE successDB calling function myOmissions with this.myP: " + this.myP);
                    this.myOmissions(this.myP);
    // IF NO INDEX IS FOUND, THEN
    	}else {
                    Mojo.Log.info("HOME SCENE successDB calling function myAdditions");
                    this.myAdditions();
    	};
    }
    Also when I was starting out programming WebOS it took me a while to figure out how to monitor the Mojo.Log.info output. All you need to do (if using a PC with Windows) is:

    1) Connect your Pre via USB to your PC and put it in Just Charge mode
    2) Go to your Start menu on your PC and Run (or search for) cmd.exe
    This will launch the Command Prompt on your PC.
    3) Copy the following code, then right-click (paste) it into the Command Prompt, then press Enter
    palm-log -d usb --system-log-level=info
    4) Now copy the following code (but first replace the name of my app with the name of your app instead) then paste this into the Command Prompt, then press Enter
    palm-log -f -d usb com.realmayo.ffplayer
    5) Now launch your app on your Pre and start using it. You will notice that the Command Prompt on your PC dynamically shows all of the Mojo.Log.info output while you are using your app.
    Last edited by RealMayo; 11/28/2010 at 02:24 PM.
    Developer of: FFplayer - a file/folder based music player. FFplayer home

Posting Permissions