javascript - Phonegap 事件简历
<p><p>我正在尝试加载 xml 数据并且一切正常,但我想在设备恢复时重新加载 xml 数据。 </p>
<p>这是我的代码,我不知道在哪里粘贴函数以恢复加载。感谢您的建议;-)</p>
<pre><code>var TITLE = "Example";
var XMLsoubor = "example.xml";
var entries = [];
var selectedEntry = "";
//listen for detail links
$(".contentLink").live("click", function () {
selectedEntry = $(this).data("entryid");
});
//Listen for main page
$("#mainPage").live("pageinit", function () {
//Set the title
$("h1", this).text(TITLE);
$.ajax({
url: XMLsoubor,
success: function (res, code) {
entries = [];
var xml = $(res);
var items = xml.find("event");
$.each(items, function (i, v) {
entry = {
title: $(v).find("id").text(),
link: $(v).find("begin").text(),
description: $.trim($(v).find("description").text())
};
entries.push(entry);
});
//store entries
localStorage["entries"] = JSON.stringify(entries);
renderEntries(entries);
},
error: function (jqXHR, status, error) {
//try to use cache
if (localStorage["entries"]) {
$("#status").html("Error");
entries = JSON.parse(localStorage["entries"])
renderEntries(entries);
} else {
$("#status").html("Error");
}
}
});
});
$("#mainPage").live("pagebeforeshow", function (event, data) {
if (data.prevPage.length) {
$("h1", data.prevPage).text("");
$("#entryText", data.prevPage).html("");
};
});
//Listen for the content page to load
$("#contentPage").live("pageshow", function (prepage) {
//Set the title
$("h1", this).text(entries.title);
var contentHTML = "";
contentHTML += entries.description;
contentHTML += '<p/><a href="' + entries.link + '"></a><br><br><br>text';
$("#entryText", this).html(contentHTML);
});
function renderEntries(entries) {
var s = '';
$.each(entries, function (i, v) {
s += '<li><a href="#contentPage" class="contentLink" data-transition="slide" data-entryid="' + i + '">' + v.title + '<br>text</a></li>';
});
$("#linksList").html(s);
$("#linksList").listview("refresh");
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>使用事件监听器来“恢复”。它应该在 deviceready 触发后立即进行。</p>
<p> <a href="http://docs.phonegap.com/en/2.9.0/cordova_events_events.md.html#resume" rel="noreferrer noopener nofollow">http://docs.phonegap.com/en/2.9.0/cordova_events_events.md.html#resume</a> </p>
<p>您的脚本中首先应该有一个 deviceready 事件,因为在触发 deviceready 事件之前,很多 Cordova 的东西还没有准备好,所以您需要监听 deviceready,如下所示:</p >
<pre><code>document.addEventListener("deviceready", onDeviceReady, false);
</code></pre>
<p>然后您在 onDeviceReady 函数中添加其他监听器并从那里开始您的应用程序的其余部分:</p>
<pre><code>function onDeviceReady() {
//The device is ready when this function is called
document.addEventListener("resume", appReturnedFromBackground, false);
}
function appReturnedFromBackground() {
//This function is called when the app has returned from the background
alert("The app has returned from the background");
}
</code></pre></p>
<p style="font-size: 20px;">关于javascript - Phonegap 事件简历,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18190475/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18190475/
</a>
</p>
页:
[1]