アップデート
一連のインスタンスを更新して多数の 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
を使用したためthat
、ajaxSuccess
関数は現在のオブジェクトのスコープで呼び出されます。したがって、ajaxSuccess
関数内では、元の同じインスタンスthis
を指します。this
私が書くとき、JavaScriptはそれを覚えていますvar that = this;