tags:

views:

26

answers:

1

My code tries repopulate options and select an element. Issues is populate does lot of task an repopulates 3 select boxes. My method control returns but when i try to select, options are not yet populated. This happens some times with slower systems / Browsers. (And specifically on IE) Is there a way to prevent this using jQuery or something ? Is there any event fired by browser when all options are loaded / or are being loaded. This seems because browser takes time sometimes to popualte options but method controls have returned.

function myMethod() {
    populateOptions();
    if(document.getElementById('Id').options[index].text==existId){
         $("#Id").val(existId);
    }
}

function populateOptions() {
//Make ajax call 
  // repopulates options
}
A: 

Since AJAX calls are asynchronous by default, you need to move your value assignment to run after the code that populates your select. Otherwise the value assignment occurs before the response comes back.

I assume whatever code populates the select is set up to run after the response is received. So placing your value selection after that code should solve the issue.

(In other words, it will go inside the populateOptions() function.)

Beyond that, it is tough to offer a solution without seeing your code.


EDIT: Here are a few examples of how this could work. Any one of these will be better than setting async: false in the request.

You can place the code that needs to wait for the response inside the success: callback.

function populateOptions() {
    $.ajax({
        url: "some/path",
        success: function( resp ) {
          // Place ANY code that needs to wait for the response in here.
          // This way it will not run until the successful response is received.
        }
    });
}

Or you could place the code that needs to wait for the response inside another function, and call that function from inside the success: callback.

function populateOptions() {
    $.ajax({
        url: "some/path",
        success: function( resp ) {
               // Call this function when the successful response is received.
            successFunction( resp );
        }
    });
}
function successFunction( resp ) {

    // Place ANY code that needs to wait for the response in here.

}

Or, say if populateOptions() should be reused in different ways, so you need a different callback, you can pass a function in from another function that will be called inside the success: callback.

function myMethod() {
      // Send a function as an argument when you call populateOptions()
    populateOptions( function(resp){alert(resp);} );

    // Code that does NOT need to wait can still go here
}
function populateOptions( callback_fn ) { // Receive the function that was sent
    $.ajax({
        url: "some/path",
        success: function( resp ) {
                // Call the function that was sent
            callback_fn( resp );
        }
    });
}

Or taking the same example as above, you could actually use the function you passed in as the success: callback.

function myMethod() {
      // Send a function as an argument when you call populateOptions()
    populateOptions( function(resp){alert(resp);} );

    // Code that does NOT need to wait can still go here
}
function populateOptions( callback_fn ) { // Receive the function that was sent
    $.ajax({
        url: "some/path",
        success: callback_fn( resp )  // Now the function passed is the callback
    });
}
patrick dw
yes. Like you said, Once we receive response we populate select and thats inside populateOptions(). So populateOptions() return control on all operations are completed. But some times browser takes time to populate options. This is in consistent with browser. If browser does it fast it works.
Jigar Shah
@Jigar - You said *"populateOptions() return control on all operations are completed"*, but that is not true when it comes to an AJAX request. An AJAX request (by default) lets the code after it continue to run even before it gets a response. So control is returned before the options are populated. This is a common situation. Just try to move your code that selects a value inside `populateOptions()` right after the code that does the populating. Or edit your question with the code for `populateOptions()`.
patrick dw
@Jigar - Was this issue resolved?
patrick dw
not exactly. But what you said is correct. Its issue as code continues till ajax response is received. So my div becomes visible even though i made it invisible. Its also a sort of browser issue. Lets say code at line 1 hides options. line 2 i make ajax call to re-populate options. But line 1 has already hidden them. Then on receiving response, why it should become visible ? Solution i used was to use jquery ajax call with async as false. so until i receive response it will wait. And receiving same. i do repopulate in success callback and then only field is hidden. So it maintains sequence.
Jigar Shah
@Jigar - It is not always good to use `async: false`. This locks up the browser, and can be an issue if a request hangs. All you really need to do is place the code that should *show* the options *inside* the `success:` callback. I'll update my answer with a few examples.
patrick dw