Results 1 to 4 of 4
  1.    #1  
    I have been programming the dirty way on webOS for a while, just hacking out code that is not modular or scalable. I would like to get away from that.

    I am trying to move much of my implementation into classes, but the asynchronous nature of the Mojo.Depot is giving me issues and I imagine that any asynchronous function will give me the same problem.

    I really want the depot transactions to block.

    In my class I have a "load" function, like this:

    Code:
    var loadHistory = function() {
    		
    		var feedingHistory = [];
    		
    		dataDepot.get("feedings",
    				function (response) {
    					//success
    					var recordSize = Object.values(response).size();
    					Mojo.Log.info("***feedings loaded via feeding Class. Size: " + Object.toJSON(response));
    					
    					if (recordSize == 0) {
    						feedingHistory = [];
    					} else if (response.length == 0) {
    						feedingHistory = [];
    					} else {
    						feedingHistory = response;	
    					}	
    					
    					if (saveInProgress.status == true) {
    						Mojo.Log.info("feedingHistory: " + Object.toJSON(feedingHistory));
    						
    						feedingHistory.push(saveInProgress.item);
    						feedingHistory.sort(function(a,b) { return parseFloat(b.startTime) - parseFloat(a.startTime) });	
    						
    						latestFeeding = feedingHistory[0];
    						
    						return saveHistory(feedingHistory);
    					}
    					
    					lastestFeeding = feedingHistory[0];
    					//return feedingHistory;
    					
    				},
    				function (t, response) {
    					//fail
    					Mojo.Log.info("***Failed to load feeding History" + response);
    					return [];
    				});
    		
    		lastestFeeding = feedingHistory[0];
    		return feedingHistory;
    	}
    The problem is that the function goes ahead and returns before the success callback executes. So, it returns "undefined".

    I want to be able to call into this class and not have it return until the data is loaded.

    Any ideas?

    I am looking into the Mojo.Function.Synchronize stuff...
    Check out my apps at clearwaterapps.com
  2. #2  
    You need to pass a callback to the function calling dataDepot.get(). Something like this:

    Code:
    var loadHistory = function(callbackSuccess, callbackFailure) {
    		
    		var feedingHistory = [];
    		
    		dataDepot.get("feedings",
    				function (response) {
    					//success
    					var recordSize = Object.values(response).size();
    					Mojo.Log.info("***feedings loaded via feeding Class. Size: " + Object.toJSON(response));
    					
    					if (recordSize == 0) {
    						feedingHistory = [];
    					} else if (response.length == 0) {
    						feedingHistory = [];
    					} else {
    						feedingHistory = response;	
    					}	
    					
    					if (saveInProgress.status == true) {
    						Mojo.Log.info("feedingHistory: " + Object.toJSON(feedingHistory));
    						
    						feedingHistory.push(saveInProgress.item);
    						feedingHistory.sort(function(a,b) { return parseFloat(b.startTime) - parseFloat(a.startTime) });	
    						
    						latestFeeding = feedingHistory[0];
    						
    						return saveHistory(feedingHistory);
    					}
    					
    					lastestFeeding = feedingHistory[0];
    					
    
    					if (callbackSuccess) {
                                                  callbackSuccess(feedingHistory);
                                            }
    				},
    				function (t, response) {
    					//fail
    					Mojo.Log.info("***Failed to load feeding History" + response);
    
    
                                            // Alternately you could use one callback for both the
                                            // onSuccess and onFailure conditions, and then pass
                                            // null back to the callback and let it check whether
                                            // the feedingHistory is not null.
    					if (callbackFailure) {
                                                  callbackFailure();
                                            }
    				});
    	}
    Quick Post: The quick way to post messages and photos to Twitter & Facebook (video link)
    Music Player (Remix): The next generation music listening experience on webOS (video link)
    GeoStrings: Set location-based reminders and never forget another task (video link)

    Twitter: @Hedami
  3. #3  
    Callbacks will set you free.

    Any one of my classes that needs to make an asynchronous call always passes as callback, usually of the form this.functionname_callback.bind(this). I even do this internally for classes that exist to interact with some service beyond my app's scope.

    I don't like anonymous functions, since if they get called more than once they can cause a brand-new object to be formed due to memory allocation.
    Doug Meerschaert
    NoInsultSoftware.com

    Apps: Scratch, GasMath, CheckMath
  4.    #4  
    Thanks guys.

    I'll dig into this a bit. I guess I have to break up the calling code into more functions, rather than relying on the order of execution of "lines" of code like I am used to.
    Check out my apps at clearwaterapps.com

Posting Permissions