2013-03-26 51 views
4

我可以为每个html页面注册'deviceready'事件吗?我使用Cordova 2.5的初始化代码,它工作正常。当我将这些代码复制到一个新的html文件时,它总是调用index.html的initialize()函数。可以在Cordova 2.5上多次听设备吗?

var app = { 
    // Application Constructor 
    initialize: function() { 
     this.bindEvents(); 
    }, 
    // Bind Event Listeners 
    // 
    // Bind any events that are required on startup. Common events are: 
    // 'load', 'deviceready', 'offline', and 'online'. 
    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 
    // deviceready Event Handler 
    // 
    // The scope of 'this' is the event. In order to call the 'receivedEvent' 
    // function, we must explicity call 'app.receivedEvent(...);' 
    onDeviceReady: function() { 
     app.receivedEvent('deviceready'); 
    }, 
    // Update DOM on a Received Event 
    receivedEvent: function(id) { 
     console.log('Received Event: XXX ' + id); 

    } 
}; 

=============== HTML文件:

....

<script type="text/javascript"> 
       app.initialize(); 
       </script> 

....

回答

6

设备准备好只会触发一次。只有当应用程序被杀害并再次打开时,它才会下次启动。

+0

...或者如果您转到手机应用程序中的另一个页面,您需要再次等待deviceready事件,对吗? (假设您没有使用单页架构)。 – 2013-04-06 20:53:31

+1

DeviceReady是一个PhoneGap事件。 PhoneGap不在乎应用程序是否为SPA。当第一次运行应用程序以发出设备准备运行应用程序自定义代码的信号时,DeviceReady被触发。 – Whizkid747 2013-04-07 00:49:14

+0

那么,根据这个[question](http://stackoverflow.com/questions/10782211/cordova-phonegap-reinitializing-on-every-page-of-jquery-mobile-app),以及我正在观察的,当我移动到一个不同的hrml页面时,它会发射..?你能补充一点解释吗? – 2014-08-04 12:30:58

相关问题