问题
I am trying to make my app (which uses Jquery Mobile 1.4) able to run
either as a Phonegap app (using Phonegap Build, not Xcode)
or as a pure webapp on a standard browser by starting my js file like below.
But none of the Jquery Mobile events are bound...
Can you help me see why ?
var deviceReadyDeferred = $.Deferred();
var jqmReadyDeferred = $.Deferred();
$(document).one("mobileinit", function () {
console.log('mobileinit just fired');
jqmReadyDeferred.resolve();
});
document.addEventListener("deviceReady", onDeviceReady, false);
function onDeviceReady() {
deviceReadyDeferred.resolve();
}
if ( isPhoneGap() ) {
alert("isPhoneGap yes");
$.when(deviceReadyDeferred, jqmReadyDeferred).then( EVERYTHING );
} else {
console.log("NOT Running on PhoneGap!");
$.when(jqmReadyDeferred).then( EVERYTHING );
}
function EVERYTHING() {
//all the JQM bindings here:
$(document).on('pagecontainershow', function (e, ui) {
//...
});
$(document).on('pagecreate','#splash-page', function(){
//...
});
$(document).on('pagecreate','#faq-page', function(){
//...
});
//etc.
}
PS: I use the function isPhoneGap from here and this works fine.
回答1:
It should not be. Your description is quite common and I could not find anything wrong. Note that you do not need to declare the Deffered object in $(document).ready() function, just let it be.
I think you need to add popup message in 2 places: onDeviceReady: to know whether the deviceready event works. EVERYTHING: to know whether these 2 Deffered object are both resolved.
BTW, I do not think isPhoneGap is a perfect one to detect whether an app is a Phonegap one or not. Sometime, you use Phonegap build to compile an app project but you forget to add cordova.js declaration, this function will still tell you "it is a Phonegap app"(This maybe current condition for you). This function is only based on the url of the link. However, as for my opinion, whether deviceready event is working is the only and best way to judge if it is a Phonegap app.
来源:https://stackoverflow.com/questions/24827726/deferred-jquery-mobile-statements-into-when-callback