2

xml データを読み込もうとしていますが、すべて正常に動作していますが、デバイスの再開時に xml データをリロードしたいと考えています。

これは私のコードですが、再開時にロードする関数をどこに貼り付けるかわかりません。アドバイスありがとうございます;-)

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

1 に答える 1

4

「再開」にはイベントリスナーを使用します。deviceready が起動したらすぐに作成する必要があります。

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

多くの Cordova の処理は、deviceready イベントが発生するまで準備ができていないため、スクリプトで最初に行う必要があるのは deviceready のイベントです。そのため、次のように deviceready をリッスンする必要があります。

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

次に、onDeviceReady 関数に他のリスナーを追加し、そこからアプリの残りの部分を開始します。

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");
}
于 2013-08-12T23:01:59.723 に答える