1

I am running this code on a DESKTOP browser (Chrome for Linux) :

var deviceReadyDeferred = $.Deferred();
var jqmReadyDeferred = $.Deferred();
document.addEventListener("deviceReady", onDeviceReady, false);
function onDeviceReady() {
        deviceReadyDeferred.resolve();
}
$(document).one("mobileinit", function () {
        console.log('JQM is ready');
        jqmReadyDeferred.resolve();
});
$.when(deviceReadyDeferred, jqmReadyDeferred).then( doWhenBothFrameworksLoaded(), failure() );
function doWhenBothFrameworksLoaded() {
            alert('success :deviceReadyDeferred is '+deviceReadyDeferred.state());
            console.log('Phonegap and JQM are loaded');
            EVERYTHING();
}

and the alert gives : success :deviceReadyDeferred is pending...whereas it shouldn't even fire, should it?

Thanks for your help.

Louis
  • 2,548
  • 10
  • 63
  • 120
  • When in doubt about promises, mock up a simple JSFiddle to test the theory: http://jsfiddle.net/TrueBlueAussie/68X6j/1/ As Arun P Johny points out you are calling the function instead of passing it. – iCollect.it Ltd Jul 18 '14 at 08:42

1 Answers1

2

You need to pass function references to the then() method as arguments, do not invoke the callback methods while calling then()(by adding () at the end of function name)

$.when(deviceReadyDeferred, jqmReadyDeferred).then(doWhenBothFrameworksLoaded, failure);

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • well actually, it seems it doesn't work that well....in my function called EVERYTHING() I put all the jquery mobile events....and the app is stuck on the first (splash) page. – Louis Jul 18 '14 at 13:19
  • adding the parenteses, generates another problem, all the jquery mobile bindings made in EVERYTHING() are not accounted for, posted a new question here : http://stackoverflow.com/questions/24827726/deferred-jquery-mobile-statements-into-when-callback – Louis Jul 21 '14 at 07:26