Utilizing Titanium MVC: Invoke function and pause for response

Currently, I am in the process of developing my very first Titanium iPhone application.

Within a model module, I have this code snippet:

(function() {   
    main.model = {};

    main.model.getAlbums = function(_args) {

        var loader = Titanium.Network.createHTTPClient();  
        loader.open("GET", "http://someurl.json"); 

        // Executes the specified function when the data is ready for processing 
        loader.onload = function() { 
            // Parse the JSON response  
            var albums = eval('('+this.responseText+')');  
            //alert(albums.length);
            return albums;
        }; 

        // Send out the HTTP request  
        loader.send();  

    };

})();

and I invoke this function in a view section as follows:

(function() {

    main.ui.createAlbumsWindow = function(_args) {

        var albumsWindow = Titanium.UI.createWindow({  
            title:'Albums',
            backgroundColor:'#000'
        });

        var albums = main.model.getAlbums();

        alert(albums);

        return albumsWindow;
    };
})();

However, it appears that the invocation to the model (which retrieves data via HTTP) does not wait for a response. In the view, when I trigger the alert, the data from the model has not been received yet. What is the recommended way to handle this situation?

Thank you in advance

Answer №1

Alright,

Here is an example,

function bar(input1, callback){
     input1 += 10;
     ....
     ... Implementation of your web service
     ....
     callback(input1); // Instead of arg1, you can have the response here
}

You would use this function as follows,

bar (initialValue, function(result){
     alert(result); // This is where you will receive the response returned from the previous function using callback(arg1);
});

In this case, input1 is a parameter (such as integer, string, etc.) and the second argument is your callback function.

Cheers.

Answer №2

In order to make a Synchronous call to a web service, it is important for the code to wait until a response is received from the service.

To implement this functionality in JavaScript, you must pass a callback function as a parameter and retrieve the return value within the callback function instead of using a return statement.

The coding style that you are currently using is unfamiliar to me as I typically use a different approach.

However, the key point is to utilize a callback function for retrieving values rather than relying on a return statement. Give this method a try and if you encounter any issues, feel free to let me know and I can provide an example for you.

Answer №3

Explaining the callback method like zero did is great, but another approach could be handling it with events.

(function() {

    main.ui.createAlbumsWindow = function(_args) {

        var albumsWindow = Titanium.UI.createWindow({  
            title:'Albums',
            backgroundColor:'#000'
        });

        var status = new object(), // eventlistener
        got_a_valid_result = false;

        // Handle result
        status.addEventListener('gotResult',function(e){
          alert(e.result);
          got_a_valid_result = true;
        });           

        // Handle error
        status.addEventListener('error',function(e){
          alert("An error occurred: "+e.errorcode);
          git_a_valid_result = true;
        });

        var albums = main.model.getAlbums(status);

        // Wait for result
        while (!got_a_valid_result){};
        return albumsWindow;
    };
})();

Your model might look something like this:

main.model.getAlbums = function(status) {

        var loader = Titanium.Network.createHTTPClient();  
        loader.open("GET", "http://someurl.json"); 

        loader.onload = function() { 
            var albums = eval('('+this.responseText+')');  

            status.fireEvent('gotResult',{result:albums});
            return albums;
        }; 

        loader.onerror = function(e){
            status.fireEvent('error',{errorcode:"An error occurred"});
        };

        // Send the HTTP request  
        loader.send();  

    };

Answer №4

To enhance security, consider using JSON.parse in place of eval when handling JavaScript code to avoid potential risks associated with running all scripts.

Answer №5

In my opinion, The Zero's solution seems to be more efficient for memory management, although I can't say for certain. If you decide to use an eventListener, it's important to note the potential implications.

function performAction(_event) {
    var foo = bar;
}
// Using this event listener may lead to a memory leak
// as references are maintained throughout the app's runtime
Ti.App.addEventListener('bad:idea', performAction);

// To prevent the memory leak, remove the event listener when the window is closed
thisWindow.addEventListener('close', function() {
// In order to remove an event listener, the same function signature must be used
// that was used when adding the listener
Ti.App.removeEventListener('bad:idea', performAction);
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting a time delay in a RESTful service

I am currently working on a service built with node.js + express. However, I am facing issues with managing the timer functionality. My service does not maintain any state, and it consists of 2 functions in the route. route.js var timerId; exports.linkU ...

Shorten a data point in JSON code

I'm currently in the process of developing a stock widget that utilizes JSON to retrieve data from the Yahoo API/YQL console. I am specifically working with values extracted from the key ChangePercentRealtime, however, the values are longer than what ...

Having trouble using Jquery Selector to locate a div element in my Polymer project. Seems like it should be simple, but I can

Purpose: My objective is to add a new class to the div that contains a form when a specific icon is clicked. Challenge: The Jquery selector in pullupClose function is not able to identify the element. Check out the console.log result for a visual represent ...

Vue Dev Tools is not updating data in Ionic Vue 3

There seems to be an issue with the updating of Reactive and Ref data in Vue Dev Tools when changes are made in an input field bound with v-model within a form. The updated data is only visible when I interact with another component and then return to the ...

Using Javascript and HTML5 Canvas to retrieve dimensions of selected objects

I have a canvas with an image containing colored squares. I want to be able to click on a square and retrieve its dimensions in pixels. For instance, clicking on a red square with a yellow border should return 24 X 100. I've come across some code that ...

The "apply" function in Javascript cannot be found in the window.external extension object

Utilizing the javascript extension (known as window.external) in IE8 (or any IE version) to expose specific functions, I encountered an issue when attempting to call the apply function. Despite the belief that it is inherently available in every JS functio ...

Switch out external CSS stylesheets automatically using toggle scripting

Hello there! I've been trying to figure out a way for this script to switch back to the original CSS external stylesheet. Here is the code that I currently have: $(document).ready(function() { $("#css-master2").click(function() { $("link[title=chang ...

Display a list of items in Angular using ng-repeat, and allow the full description to appear in full width

<div ng-controller = "MyController"> <ul class="items" > <div ng-repeat="item in colors" ng-class="{active:isActive(item)}" ng-click="select(item); whattoshow=!whattoshow"> <li class="col-md-3 col-sm-3 col-lg-3 co ...

The reference to 'this' object is not functioning properly, indicating that either a 'property' or 'method' is undefined when attempting to access a method through routes

I am encountering an issue with the this reference when calling a method via a route. For example, when I call /api/certifications, it triggers the indexAction method. Within this method, I try to access a property using this.collection but receive an err ...

Sort through a collection of arrays that each contain objects

Having a challenge filtering a multidimensional array with objects within the inner arrays. Despite following examples found here, I'm still unable to successfully filter my array as desired. let arr = [ { name: 'brent', age: 123 } ]; Alth ...

Refresh a specific DIV element without having to refresh the entire page

I have a Div Tag that includes Small.php to populate it with information. My goal is to refresh the content every 15 seconds without reloading the entire webpage. I've attempted using JavaScript/jQuery without success. <script type="text/javascrip ...

Attention all controllers summoned from one AngularJS document

Having recently delved into the world of AngularJS and Ionic, I've exhaustively searched for solutions both on this forum and beyond. Despite my efforts, nothing seems to be working. My goal is to create an application with a homepage featuring a ser ...

"Utilizing Bootstrap to create a multiselect feature that can dynamically

I am facing an issue with receiving alerts for dynamically added bootstrap select elements <html> <head> <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> <link hr ...

Surprising Regex Match of ^ and special character

My regular expression is designed to break down calculator input strings, such as 12+3.4x5, into individual tokens like 12, +, 3.4, x, and 5 Here is the regular expression I am using: \d+\.?\d+|[\+-÷x] However, I am encountering une ...

Revamp the code by implementing promises

Upon calling the code snippet below, the two Webtrends calls are getting cancelled instead of returning an HTTP 200 status. This happens because the form submission triggers the cancellation. To resolve this issue, I introduced a 2-second delay before subm ...

HTML5 Mouse Canvas

Here's a simple example of what's happening: function handleClick(event) { ... } canvas.addEventListener("click", handleClick, false); function drawRectangle(x, y) { context.fillRect(x, y, 16, 16); }; ...

How can I programmatically close the Date/Time Picker in Material UI Library?

Is there a way to programmatically close the Date/Time Picker in Material UI Library? ...

Dividing a string in JavaScript to generate fresh arrays

I am currently dealing with the following string: "ITEM1","ITEM2","ITEM3","ITEM4","ITEM5","ITEM6","ITEM7"~100000000,1000048,0010041,1,1,1,1~100000001,1000050,,2,0,2,1~100000002,1068832,0 ...

What causes bootstrap to fail on smaller screens?

While developing an app using Bootstrap, I encountered an issue where the app was not functioning properly on small screens. Everything worked perfectly on larger screens, such as a PC browser, but on a mobile browser, none of the tabs would open. When I t ...

Troubleshooting Problems with IndexOf in Javascript/Google Apps Script

Although I've found similar questions about the IndexOf function, none of the answers have resolved my specific issue. In my case, I have a large 2D array containing names and ID codes from a spreadsheet, which I read into an array named rIdr in my a ...