到目前为止,我正在构建我的第一个移动应用程序,但不知道我的解决方案是否好。
我使用了一个模板(html 文件),只有一个 data-role="page"。每个内容元素只是一个 div,可以通过 javascript 隐藏或显示。 请检查以下代码以使其清楚。这也是我的应用程序的入口点。
$(document).delegate("#index", "pageinit", function(event, ui) {
initRotation();
initNavigation();
initImpressum();
initService();
cookie = null;
......
isLoggedIn(); //returns cookie = true || false
if (cookie == null) {
$('#login').show();
$('#home, #foot, #service').hide();
initRegistration();
} else {
$('#login, #service').hide();
$('#foot, #home').show();
$('#naviHome').addClass("ui-btn-active"); // ui-state-persist?!
createHomeContentFromServer();
}
});
我现在有几个问题。
- 该应用有点慢。
- Pageinit 事件被频繁触发。你能阻止它吗?
- 代码对于第三方来说不太可读,因为我有大约 100 个外部 javascript 文件。 (每次用户执行某操作时,大约 500 个 div 会被隐藏,10 个会显示)
- 您有具有良好架构的应用的完整示例吗?
最佳答案
您的应用程序速度很慢,因为它每次都加载一大页。页面初始化会多次触发,可能是因为每次单击链接时都会重新加载一大页。
在 JQM 中,您有许多 data-role="page",而不仅仅是一个。下面是Dreamweaver CS6 生成的标准模板。它使用 JQ 和 JQM CDN,格式为四页。它应该给你一个好的开始:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery Mobile Web App</title>
<link href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" rel="stylesheet" type="text/css"/>
<script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js" type="text/javascript"></script>
</head>
<body>
<div data-role="page" id="page">
<div data-role="header">
<h1>Page One</h1>
</div>
<div data-role="content">
<ul data-role="listview">
<li><a href="#page2">Page Two</a></li>
<li><a href="#page3">Page Three</a></li>
<li><a href="#page4">Page Four</a></li>
</ul>
</div>
<div data-role="footer">
<h4>Page Footer</h4>
</div>
</div>
<div data-role="page" id="page2">
<div data-role="header">
<h1>Page Two</h1>
</div>
<div data-role="content">
Content
</div>
<div data-role="footer">
<h4>Page Footer</h4>
</div>
</div>
<div data-role="page" id="page3">
<div data-role="header">
<h1>Page Three</h1>
</div>
<div data-role="content">
Content
</div>
<div data-role="footer">
<h4>Page Footer</h4>
</div>
</div>
<div data-role="page" id="page4">
<div data-role="header">
<h1>Page Four</h1>
</div>
<div data-role="content">
Content
</div>
<div data-role="footer">
<h4>Page Footer</h4>
</div>
关于javascript - Phonegap 和 jQuery Mobile 架构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14891082/