0

I'm using Cordova (Drupalgap) to make a mobile app for my website. I'm a Javascript novice, and I have a function that creates a page (mymodule_home_auth_page()). Inside that function, I want to call another function, mymodule_get_unread_pm_count() (to check whether the user has unread messages), and, if this function returns a value, I want to add some markup to content[] inside the parent function.

However, the content[] markup in the function-inside-the-function is not printed to the display. The function mymodule_get_unread_pm_count() does return a value because when I load the app the alert() is called, informing me I have unread messages, but the markup is not printed to the display.

I assume this has something to do with scope, but I don't understand what I need to change to allow me to inject something into content[] in the parent function.

Here is the code I am using now:

function mymodule_home_auth_page() {
  try {
    var content = {};
    content['header'] = {
      markup: '<h1>Hello world</h1>',
      }
    };
    mymodule_get_unread_pm_count({
      success: function(result) {
        content['unreadmessages'] = { markup: '<h2>You have unread messages</h2>'};
        var num_unread = result[0];
        alert('You have' + num_unread + 'unread messages.');
        return content;
      }
    });
    content['footer'] = {
      markup: '<p>Nothing to see down here.</p>',
      }
   };
  catch (error) { console.log('mymodule_home_auth_page - ' + error); }
}
Patrick Kenny
  • 4,515
  • 7
  • 47
  • 76
  • 1
    http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call - similar because both are regarding async processing – Arun P Johny Feb 26 '15 at 09:33

1 Answers1

0

Thanks, based on Arun P Johny's comment, I was able to do it by calling mymodule_get_unread_pm_count() in a function that is called on log in, which I used to set a variable containing the number of unread messages. That way, I could use an if statement inside mymodule_home_auth_page() instead of calling another function, thereby eliminating the problem.

Patrick Kenny
  • 4,515
  • 7
  • 47
  • 76