2013-03-26 9 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

相關問題