1

まず最初に、私が提供しようとしている情報が十分かどうかはわかりません。必要に応じて追加情報を喜んで追加します。

複雑な構造を JSON 形式にシリアル化しています。Field[i][0] は、オブジェクトへの「this」参照です。

JSON.Stringify(myObj) での Firebug の出力

Firebug プリントスクリーン

これは、すべて JS を保持している限り、問題なく動作します。しかし今、シリアル化してバックエンドに送信し、参照と計算された情報を取得する必要があります。

では、以前に持っていた参照にどのようにマップし直せばよいでしょうか? この参照をオブジェクトにバインドするにはどうすればよいですか? この $$hash は内部的で独占的なものに見えるので、Object[$$hash] = ref などを試すことさえ気にしませんでした。

この一般的な考え方はかなり奇抜に思えるかもしれませんが、結果は非同期的に返されるため、新しい情報を元のオブジェクトにバインドするための識別子が必要です。明らかに、そのための独自の識別子を作成することもできますが、この方法で解決するオプションがあるかどうか疑問に思っていました.

編集

オブジェクトはこのように作成されます(同様に)

var arrayOfObj = []

arrayOfObj.push(new Object.With.SomeSettersAndGetters());

オブジェクトには次のようなメソッドがあります

function GetRef(){
return this;
}

コードを通じてID / Refを保持するために使用しています。

ありがとうございました!

4

1 に答える 1

1

アップデート

一連のインスタンスを更新して多数の Ajax リクエストを作成する場合は、Ajax のロング ポーリングとキューイングの手法を検討する必要があります。参照を保存することはできませんが、使用する Ajax 手法に関係なく、以下のトリックを使用して参照を保存してください。

上に長いポーリングを追加すると、準備完了です。

アイデアは次のとおりです。

サーバーが JSON 形式で応答するとします。元の参考文献を参照する必要がある場合は、ここに私の 2 セントを示します。

サーバーが応答したときに正確な参照を更新します。Something配列に10 個のインスタンスが格納されているとします。応答が成功したら、Somethingクラスのメソッドを使用して、特定のインスタンスを任意の方法で更新します。

  /**
   * The array with something instances.
   * @type {Array.<Something>}
   */
  var instances = [];

  /**
   * The Ajax success function.
   * @param {Event} event The event object.
   */
  function ajaxSuccess(event) {
       var response = event.target.getResponseText();
       var actualResponse = JSON.parse(response);
       for (var i = 0, len = actualResponse.length; i++) {
           instances[i].setWhatever(actualResponse[i].whatever);
       };
    };

上記は、より手続き的なアプローチです。JS で本格的な OOP が必要な場合は、モジュラー デザイン パターンを考えます。データをどこかにロードするモジュールがあるとします。基本的に、そのモジュールに関連するものはすべてインスタンス プロパティです。

var myModule = function() {
    this.whatever = 1;
};
myModule.prototype.loadMore = function() {
    var request = new XMLHttpRequest(),
        that = this; // store a reference to this.
    request.send(); // etc
    request.onreadystatechange = that.onSucess;
};
myModule.prototype.onSucess = function(event) {
    var response = JSON.parse(event.target.getResponseText());
    this.whatever = response.whatever;

};
var moduleInstance = new myModule();
myModule.loadMore();
// Now the scope is always preserved. The callback function will be executed in the right scope.

バックエンド側に、クライアント側の JavaScript モデルを模倣するモデル クラスがあるとします。テキストを表示するモデル内の参照を更新するとします。バックエンドで Scala を使用していますが、フィールド/プロパティを見て構文を無視しています。

case class Article (
    title: String,// these are my DB fields for an Article.
    punchline: String,
    content: String,
    author: String
);
// now assume the client is making a request and the server returns the JSON
// for an article. So the reply would be something like:
{"title": "Sample title", "punchline": "whatever", "content": "bla bla bla boring", "author": "Charlie Sheen"};
// when you do 
var response = JSON.parse(event.target.getResponseText());
// response will become a JavaScript object with the exact same properties.
// again, my backend choice is irrelevant.

// Now assume I am inside the success function, which gets called in the same scope
// as the original object, so it refers TO THE SAME THING.
// the trick is to maintain the reference with var that = this.
// otherwise the onSuccess function will be called in global scope.
// now because it's pointing to the same object.
// I can update whatever I want.
this.title = response.title;
this.punchline = response.punchline;
this.content = response.content;
this.author = response.author;
// or I can put it all in a single variable.
this.data = response;

覚えておく必要があるのは、スコープを保持する必要があるということです。それがコツです。その際var that = this;、モデル インスタンスへの参照をコピーします。参照は、現在のスコープではなく、高次を通じて記憶されます。

次に、オブジェクトが完了したらXMLHttpRequest呼び出すように指示します。that.ajaxSuccessを使用したためthatajaxSuccess関数は現在のオブジェクトのスコープで呼び出されます。したがって、ajaxSuccess関数内では、元の同じインスタンスthisを指します。this

私が書くとき、JavaScriptはそれを覚えていますvar that = this;

于 2013-04-29T12:02:07.797 に答える