javascript 我是否需要在cordova/phonegap 项目中的所有html 文件中添加app.initialize()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16442922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need to add app.initialize() in all my html files in a cordova/phonegap project
提问by Lali Pali
I am making a phonegap/cordova project. I created a skeleton project using command line, as the guidesuggests to making a new android/phonegap project.
我正在制作一个 phonegap/cordova 项目。我使用命令行创建了一个骨架项目,因为指南建议创建一个新的 android/phonegap 项目。
In the index.html file created there is a piece of code app.initialize()
, and the code it comes from a file called index.js.
在创建的 index.html 文件中有一段代码app.initialize()
,它来自一个名为 index.js 的文件。
My question is, do I have to have this piece of code in all my html files, since i will be using jQueryMobile to do the front-end, I might need to have several html files.
我的问题是,我是否必须在我的所有 html 文件中都有这段代码,因为我将使用 jQueryMobile 来做前端,我可能需要有几个 html 文件。
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) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
采纳答案by Lali Pali
Since all pages are called through Ajax calls, in theory you don't need to add that line in all your pages. But in some cases you might want to add it, for example if there might be a chance that the particular page might not be called from an ajax call, or a user for some strange reason lands on that page, instead of your index page.
由于所有页面都是通过 Ajax 调用来调用的,理论上您不需要在所有页面中都添加该行。但在某些情况下,您可能想要添加它,例如,如果特定页面可能不会从 ajax 调用中调用,或者用户出于某种奇怪的原因登陆该页面,而不是您的索引页面。