Phonegap事件恢复 [英] Phonegap event Resume

查看:51
本文介绍了Phonegap事件恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载xml数据,并且一切正常,但是我想在设备恢复时重新加载xml数据。

I am trying to load xml data and everything works good, but I would like reload xml data when device resume.

这是我的代码,我不知道在哪里粘贴函数以加载简历。感谢您的建议;-)

This is my code, and I don't know where to paste function for load on resume. Thanks for advices ;-)

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[selectedEntry].title);
    var contentHTML = "";
    contentHTML += entries[selectedEntry].description;
    contentHTML += '<p/><a href="' + entries[selectedEntry].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");
}


推荐答案

将事件监听器用于恢复。

Use the eventlistener for "resume". It should be made as soon as deviceready has fired.

http://docs.phonegap.com/en/2.9.0/cordova_events_events.md.html#resume

在脚本中应该首先要做的是设备就绪事件,因为在设备就绪事件被触发之前,许多Cordova事情还没有准备好,因此您需要监听设备就绪,就像这样:

The very first thing you should have in your script is an event for deviceready because a lot of Cordova things aren't ready until the deviceready event has been fired, so you need to listen for deviceready, like this:

document.addEventListener("deviceready", onDeviceReady, false);

然后在onDeviceReady函数中添加其他侦听器,然后从此处开始其余应用程序的启动:

Then you add the other listeners in the onDeviceReady function and begin the rest of your app start up from there:

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");
}

这篇关于Phonegap事件恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆