1

I´m working on an app using PhoneGap, JQuery

I have 2 standard javascript functions.

LoadLanguage() will make an Ajax call to get a language file and then set up a Javascript object.

SetLanguage() will use the previous object (set in LoadLanguage()) and then set some variables with the correct language.

So in my js file i have this code:

function LoadLanguage()
{
    ....make ajax call and set up an object
}

function SetLanguage()
{
    ....set language based of previous LoadLanguage()
}

$( '#startpage' ).live( 'pagebeforecreate',function(event)
{
    LoadLanguage();
    ....
}

$( '#startpage' ).live( 'pagebeforeshow',function(event)
{
    SetLanguage();
    ....
}

Problem is that the SetLanguage() will fire before LoadLanguage() is done. I could solve this problem running SetLanguage() inside the LoadLanguage ajax.complete. But this is not good since i need to call SetLanguage() in other parts of my code.

So how do i solve this in a good way?

Vishal
  • 1,236
  • 1
  • 9
  • 16
Juw
  • 2,059
  • 4
  • 18
  • 23
  • Because ajax is asynchronous the only method is as you have identified using it as a callback in the `ajax.complete` function. You can still call `SetLanguage()` in other parts of the code as long as it is in scope. – George Reith Sep 04 '12 at 12:45

5 Answers5

2

You need to call the SetLanguage() function once the ajax request is completed and response is received. if you are using jquery then in LoadLanguage function where you are making ajax request

success: function(response)
{
    SetLanguage();
}
Jignesh
  • 471
  • 6
  • 17
  • Yeah, i used it like that...but didn´t read you post until know. But you will get the "Accepted answer tick". Thanx for your help. – Juw Sep 05 '12 at 12:48
1

You can set async property of ajax call to false. This might be helpful: How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

Community
  • 1
  • 1
Ruchit Rami
  • 2,273
  • 4
  • 28
  • 53
  • I would advise against this, ask yourself why are you using AJAX? See [When is it appropriate to use synchronous ajax](http://stackoverflow.com/questions/4316488/when-is-it-appropriate-to-use-synchronous-ajax) For more info – George Reith Sep 04 '12 at 13:02
  • false will be removed in JQuery 1.8 so i don´t want to use it. – Juw Sep 04 '12 at 13:15
1

The method you are proposing seems fine to me. Your question - or more precisely, your point of interest seems to be language agnostic.

As I understand it, you believe there is an issue around calling the SetLanugage function from other parts of your script, and also after the LoadLanguage function. I don't see an issue here at all. If it is safe to call SetLanguage from other parts of the script, then after loading a language, it should behave the same.

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • It wasn´t really an issue. I messed around with it a little more then it had to be. But thx for your answer. – Juw Sep 05 '12 at 12:49
1

You could also set a global variable outside of your function calls that will only become true when the language has been loaded. Then test for this inside SetLanguage.

James Tomasino
  • 3,520
  • 1
  • 20
  • 38
  • I could do that. But it doesn´t seem like a nice solution. Because if it else false. I need to take care of it somehow. – Juw Sep 05 '12 at 12:50
1

if you want to lock down the ajax call use this

if($.active){ 
  //there's an ajax function running
}else{ 
  //there's no ajax function running
} 
Anil D
  • 1,989
  • 6
  • 29
  • 60