2013-03-26 1 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 이벤트를 기다려야합니까? (단일 페이지 아키텍처를 사용하지 않는다고 가정). –

+1

DeviceReady는 PhoneGap 이벤트입니다. 응용 프로그램이 SPA인지 아닌지 PhoneGap은 걱정하지 않습니다. DeviceReady는 앱이 앱 실행 맞춤 코드를 실행할 준비가되었음을 알리기 위해 처음 실행될 때 시작됩니다. – Whizkid747

+0

글쎄,이 [질문] (http://stackoverflow.com/questions/10782211/cordova-phonegap-reinitializing-on-every-page-of-jquery-mobile-app)에 따르면, 그리고 내가 뭘 관찰하고있어, 내가 다른 hrml 페이지로 이동할 때마다 발사됩니다 ..? 약간의 설명을 추가 할 수 있습니까? –

관련 문제