ユーザーが私のブログの並べ替えリンクをクリックしたときに、AJAXを使用して新しい投稿を取得し、並べ替えリンクセクションを更新したいと思います。これはRJSを使用すると簡単ですが、JSを目立たなくし、データのみ(コードなし)でAJAXリクエストに応答したいと思います。
これが私が今していることです:
$('a.order_by').livequery('click', function() {
$.get(this.href, null, function(data) {
// The server returns a blob of HTML with a div#posts
// and a div#sorting
$("#posts").partial_html(data, "#posts");
$("#sorting").partial_html(data, "#sorting");
});
return false;
});
そしてここにpartial_html()
機能があります:
// Stolen from the jQuery source for $.load()
jQuery.fn.partial_html = function(data, selector) {
$(this).html($("<div/>").append(data.replace(/<script(.|\s)*?\/script>/g, "")).find(selector));
}
明らかに、これは少し嫌です-これを行う正しい方法は何ですか?(2つを使用するのは非常に簡単$.load()
ですが、余分なHTTPリクエストが必要になります)。