3

我从 jQuery mobile 和 Phonegap 开始。

我有一个类别选择器,并根据所选类别显示属于该类别的复选框列表。请参阅下面我可能会显示的复选框组的 1 个示例。请注意,它是隐藏的,如果用户在“类别”字段中选择“位置”,我只会显示该字段。

<select name="select-choice-0" id="category" onchange="javascript:showboxes();updateresults();">
<option value="0">Select a category</option>
<option value="4">Catering</option>   
<option value="1">Locations</option>
<option value="2">Music</option>
</select>


<div id="locationthemes_box" data-role="collapsible" style="display:none;">
<h3>Themes</h3>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
    <legend>Theme</legend>
    <input type="checkbox" name="locationthemes" id="checkbox-1" value="2" class="custom" />
    <label for="checkbox-1">Castle</label>
    <input type="checkbox" name="locationthemes" id="checkbox-2" value="3" class="custom" />
    <label for="checkbox-2">Barn</label>
    <input type="checkbox" name="locationthemes" id="checkbox-3" value="5" class="custom" />
    <label for="checkbox-3">Restaurant</label>
    <input type="checkbox" name="locationthemes" id="checkbox-4" value="8" class="custom" />
    <label for="checkbox-4">Bar</label>
</fieldset>
</div>
</div>

现在,我有更多想要显示或隐藏的复选框组,具体取决于所选类别。我想防止我拥有页面中未显示的复选框的 HTML。因为这会导致不必要的带宽使用。然后我想:也许我应该通过在用户更改类别选择框后立即对相关复选框和值发出 Web 服务请求来动态加载复选框组的内容。

但这会导致大量的网络请求,我试图节省带宽。

所以我的想法是本地存储可能能够解决我的问题。然后我会将复选框值存储为名称/值字符串:
locationthemes = 2;Castle;3;Barn;5;Restaurant;8;Bar

我的问题:这是要走的路还是有我不知道的最佳实践?
如果我的方式是建议的方式:欢迎使用 PhoneGap 使用本地存储的任何示例!

4

2 回答 2

2

如果您使用 Jquery Mobile 的单页概念,即拥有一个 HTML 并包含其中的所有页面,如下所示

<div data-role="page" id="page1">
<div data-role="page" id="page2">
<div data-role="page" id="page3">
<div data-role="page" id="page4">

然后您可以使用全局变量来存储值。

否则,您可以按照此处的说明使用 localStorage http://docs.phonegap.com/en/1.8.1/cordova_storage_storage.md.html#localStorage

于 2012-07-02T13:35:18.827 回答
0

我建议使用 localStorage 只是因为 Android 在需要时会无情地杀死您的应用程序。一旦用户将您的应用程序置于后台一段时间,或者如果他让手机进入睡眠状态或等待时间过长,那么该应用程序可能会被杀死。在这里,您可以确保始终将应用程序的状态或重要变量存储在 localStorage 中,然后在重新启动应用程序时再次获取此数据(通过简单的启动检查)。

于 2013-02-09T10:01:34.000 回答