2013-07-23 108 views
4

我无法获得设备准备好的功能,在手机内工作,即xcode模拟器。的HTML是如下:` Phonegap不调用设备就绪功能

<title>Boilerplate</title> 
</head> 
<body> 

    <div id="main" data-role="page"> 
     <div data-role="header" class="logo"> 
      <img class="logo" src="img/premium-logo.jpg" /> 
     </div> 

     <div data-role="content"> 

      <h1>Apache Cordova Test Zone</h1> 
      <div class="test-zone" id="test-zone"> 

      </div> 

     </div> 

     <div data-role="footer"> 

      <h4>Footer of main page</h4> 

     </div> 

    </div> 



    <script type="text/javascript" src="js/jquery-2.0.3.min.js"></script> 
    <script type="text/javascript" src="js/jQuery-Mobile-1.3.1-min.js"></script> 
    <script type="text/javascript" src="cordova-2.3.0.js"></script> 
    <script type="text/javascript" src="js/index.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(init()); 
    </script> 
</body> 

JavaScript文件index.js:

function init() { 
    document.addEventListener("deviceready", onDeviceReady, false); 
} 

function onDeviceReady() { 
    alert('It works!'); 
} 

如果我注释掉初始化函数内部的线路和更换它只需使用onDeviceReady();我可以获得警报以使用chrome。

为什么它不能在模拟器中使用上面的代码。 谢谢

+1

我会尝试注册' document.addEventListener(“deviceready”,onDeviceReady,false);'$(document).ready()'外部''。除此之外'$(document).ready()'将'function'作为参数,例如。 '$(document).ready(init);' – twil

+0

我试着调用document.addEventListener(“deviceready”,onDeviceReady,false);我已经准备好了在设备上调用init函数。仍然无法看到这是不是在工作 – psycho

+0

那么,什么是日志?我没有机会使用iOS的PhoneGap,但与Adnroid它并没有因为破解JS而被解雇了几次,我可以在调试日志中看到它 – twil

回答

6

onDeviceReady事件只从设备模拟器测试您的PhoneGap应用程序时,无法在Chrome工作。

这里是我发现做两个框架(phonegap和jQuery Mobile)一起工作的最佳方式。

在我的index.html

<script type="text/javascript" src="js/libs/LABjs/LAB.min.js"></script> 
<script type="text/javascript" src="js/libs/jQuery/jquery-1.9.1.js"></script> 
<script type="text/javascript" src="js/index.js"></script> 
<script type="text/javascript" src="js/libs/jQuery/jquery.mobile-1.3.1.js"></script> 

请注意我用的是LABjs库加载JS脚本(cordova.js正在被只有当我们发现,我们是在一个设备载入),你可以在Google的LABjs库中找到它。

在我 “的js/index.js”

var deviceReadyDeferred = $.Deferred(); 
var jqmReadyDeferred = $.Deferred(); 
var resourcesReady = false; 


var app = { 
    // Application Constructor 
    initialize: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 

     //load scripts 
     if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) { 
      $LAB.script("cordova.js").wait(
       function(){ 
        document.addEventListener('deviceready', this.onDeviceReady, false); 
        console.log('We are on Device'); 
       } 
      ); 
     }else 
     { 
      console.log('We are on Browser'); 
      var _this = this; 
      setTimeout(function(){ 
       _this.onDeviceReady(); 
      }, 1); 
     } 

     console.log('app.initialize() Called'); 
     $.when(deviceReadyDeferred, jqmReadyDeferred).then(this.doWhenBothFrameworksReady); 
    }, 

    // deviceready Event Handler 
    onDeviceReady: function() { 
     console.log("onDeviceReady"); 
     deviceReadyDeferred.resolve(); 
    }, 

    doWhenBothFrameworksReady: function() 
    { 
     console.log("doWhenBothFrameworksReady"); 
     resourcesReady = true; 
    } 
}; 

$(document).one("mobileinit", function() { 
    $.support.cors = true; 
    $.mobile.allowCrossDomainPages = true; 
    $.mobile.phonegapNavigationEnabled = true; 
    console.log('MobileInit'); 
    jqmReadyDeferred.resolve(); 
}); 



function PageShowFunction(e) 
{ 
    // we are sure that both frameworks are ready here 
} 

function CallPageEvent(funcToCall,e) 
{ 
    if(resourcesReady) 
    { 
     return funcToCall(e); 
    }else 
    { 
     setTimeout(function() { 
      CallPageEvent(funcToCall,e); 
     }, 200); 
    } 
} 


$(document).ready(function() { 
    console.log("document ready"); 
    // ALL the jQuery Mobile page events on pages must be attached here otherwise it will be too late 
    // example: 
    // I use the CallPageEvent beacause this event could be called before the device ready 
    /* 
    $("#page").on("pageshow", function(e) { 
       CallPageEvent(PageShowFunction,e); 
      } 
    */ 



}); 

app.initialize(); 
+0

是的,我知道我只是说我用chrome来检查函数是被调用的,但它仍然不能在IOS模拟器上工作。它会加载应用程序,但不会显示onDeviceReady函数中的警报消息 – psycho

+1

问题在于,当您在文档的“准备好”事件中设置事件时,onDeviceReady函数已被触发。 Phoneap和jquery mobil在使用togheter时遇到了这个问题,我将发布一个使用两个框架的完整方法。 – ElLocoCocoLoco

+0

感谢您的时间和精力 – psycho

0

只使用

<script type="text/javascript" src="js/index.js"></script> 
<script type="text/javascript"> 
    init(); 
</script> 

INSTEAD OF

<script type="text/javascript" src="js/index.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function(){ init(); }); 
</script>` 
+0

谢谢,但我已经尝试过,但没有快乐 – psycho

3

添加

<script type="text/javascript" src="cordova.js"></script> 

您的index.html文件中,工作对我来说)