0

phpを使用してIDで情報をランダム化し、xml経由で送信しているデータベースがあります。私の問題は、xmlを1回だけ取得し、少なくとも2つの関数で使用するために保存したいということです... 1つの関数はonloadを実行してxmlの最初の行を取得し、もう1つはボタンを押してアクセスするたびに実行します最後までxmlの次の行。私の2つの関数はloadfirst()とloadnext()です。loadfirst()は完全に機能しますが、xmlデータをloadnext()に渡す方法がわかりません。現在、pageloadでloadfirst()を使用し、ボタンを押すとloadfirst()を使用していますが、データベースから毎回新しいxmlを作成することになり、ランダム化の問題が発生し、非常に非効率的です。どんな助けでもいただければ幸いです。

var places;
var i = 0;

function loadXML(){

downloadUrl("places.php", function(data){
    places = data.responseXML;
        getFeatured(i);
});



}

function getFeatured(index){
   var id = places[index].getAttribute("id");
    var name = places[index].getAttribute("name");
    var location = places[index].getAttribute("location");
    var imgpath = places[index].getAttribute("imgpath");
    var tags = places[index].getAttribute("tags");

}


    function getPrev() {
    i--;
    getFeatured(i);
}

 function getNext() {
    i++;
         getFeatured(i);
    }


function downloadUrl(url, callback) {
     var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;
  request.onreadystatechange = function() {
        if (request.readyState == 4) {
            request.onreadystatechange = doNothing;
            callback(request, request.status);
        }
     };
     request.open('GET', url, true);
     request.send(null);
   }

    function doNothing() {}

loadnext()はloadfirst()と非常によく似ていますが、データベースに再度アクセスしなくても使用できるように、xmlデータの受け渡しで問題が発生しています。ありがとう。

4

3 に答える 3

1

xmliをパブリックスコープに設定します。次に、iをインクリメント/デクリメントし、XMLからデータを再読み取りするだけです。このようなもの:

var xml;
var xml_idx = 0; // replaces your i counter

function loadXML() {
    downloadUrl ("places.php", function(data) {
        xml = data.responseXML;
    )};
}

function loadItem(index) {
    var id = xml[index].getAttribute("id");
    var name = xml[index].getAttribute("name");
    var location = xml[index].getAttribute("location");
    var imgpath = xml[index].getAttribute("imgpath");
    var tags = xml[index].getAttribute("tags");
    // do something with this data
}

function loadCurrentItem() {
    loadItem(xml_idx);
}

function loadNextItem() {
    xml_idx++;
    loadItem(xml_idx);
}

function loadPreviousItem() {
    xml_idx--;
    loadItem(xml_idx);
}

// usage
loadXML();  // do this first to populate xml variable
loadItem(xml_idx); // loads first item (i=0)
loadCurrentItem(); // loads i=0
loadNextItem(); // loads i=1
loadNextItem(); // loads i=2
loadPreviousItem(); // loads i=1

本当に凝ったものにしたい(そしてグローバル名前空間をよりクリーンに保ちたい)場合は、これを簡単にクラスにすることができます。

于 2012-12-07T19:15:22.377 に答える
0

スコープとデータの状態を制御するには、これらすべてを1つのオブジェクトにラップする必要があります。(以下のテストされていないコード。使用する可能性のあるパターンとインターフェースを示しているだけです。)

function PlacesScroller(url, callback) {
    this.url = url;
    this.data = null;
    this._index = null;
    this.length = 0;
    var self = this;
    downloadURL(this.url, function(result, status) {
        if (Math.floor(status/100)===2) {
            self.setData(result);
        }
        if (callback) {
            callback(self, result);
        }
    });
}
PlacesScroller.prototype.setData(xmldom) {
    this._index = 0;
    // this may require changing; it depends on your xml structure
    this.data = [];
    var places = xmldom.getElementsByTagName('place');
    for (var i=0; i<places.length; i++) {
        this.data.push({
            id : places[i].getAttribute('id'),
            name : places[i].getAttribute('name')
            // etc
        });
    }
}
PlacesScroller.prototype.getPlaceByIndex = function(index) {
    if (this.data) {
        return this.data[index];
    } else {
        return null;
    }
}
PlacesScroller.prototype.getCurrentFeature = function() {
    return this.getPlaceByIndex(this._index);
}
PlacesScroller.prototype.addToIndex(i) {
   // This sets the index forward or back
   // being careful not to fall off the end of the data
   // You can change this to (e.g.) cycle instead
   if (this.data===null) {
        return null;
   }
   var newi = i+this._index;
   newi = Math.min(newi, this.data.length);
   newi = Math.max(0, newi);
   this._index = newi;
   return this._index;
}
PlacesScroller.prototype.getNextFeature = function() {
    this.addToIndex(1);
    return this.getCurrentFeature();
}
PlacesScroller.prototype.getPreviousFeature = function() {
    this.addToIndex(-1);
    return this.getCurrentFeature();
}

次に、それを初期化し、次のように使用します。

var scroller = new PlacesScroller('places.php', function(scrollerobject, xmlresult){
   // put any initialization code for your HTML here, so it can build after
   // the scrollerobject gets its data.
   // You can also register event handlers here

   myNextButton.onclick = function(e){
        var placedata = scrollerobject.getNextFeature();
        myPictureDisplayingThing.update(placedata);
   }
   // etc
});
于 2012-12-07T21:05:16.333 に答える
0

グローバル変数(items-items array、iterator-counter)を使用して、すべての関数で使用可能なデータを格納します。
次のようなものを試してください。

items = false;
iterator = 0;

function loadfirst(){

downloadUrl ("places.php", function(data) {
    var i = 0;
    var xml = data.responseXML;
    var places = xml.documentElement.getElementsByTagName("place");
    var id = places[i].getAttribute("id");
    var name = places[i].getAttribute("name");
    var location = places[i].getAttribute("location");
    var imgpath = places[i].getAttribute("imgpath");
    var tags = places[i].getAttribute("tags");

    items = places;
    iterator++;
)};
}


function loadnext(){


    var i = iterator;
    var id = items[i].getAttribute("id");
    var name = items[i].getAttribute("name");
    var location = items[i].getAttribute("location");
    var imgpath = items[i].getAttribute("imgpath");
    var tags = items[i].getAttribute("tags");

    iterator++;

}
于 2012-12-07T19:12:04.083 に答える