jquery.simplejsonrest.js 最終案

// Simple JSON-REST plugin for jQuery

//low level

$.SJR.option = {
	"success" : function(response, status){alert('success');},
	"error"   : function(xhr, status, thrown){alert('error');}
};

$.SJR.option.type = 'POST';
$.SJR.option.url  = '/_je/myDoc';
$.SJR.option.data = '{"foo":"bar"}';

$.SJR.ajax();

//基本

option  = {
	"url"  : "/_je/myDoc",
	"data" : {"foo":"bar"} // or "foo=bar"
};
success = function(response, status){alert('success')};
error   = function(xhr, status, thrown){alert('error');};

$._GET (option, success, error);
$._POST(option, success, error);
$._PUT (option, success, error);
$._DELETE(option, success, error);

//jQuery的な使い方

//要素の内容を元に実行される。内容を走査する為のコーディングは不要。
$('form')._POST(success, error);
$('form')._PUT (success, error);
$('form')._DELETE(success, error);

//取得したデータを要素の内容に反映させる。基本的に内容を反映させるコーディングは不要。
id = '_docId=00roxf3RwEBi1ZobhYECZdUnT766oU97';
$('form')._GET(id, success, error);

//取得したデータを要素の内容に反映させるコーディング例。
//それぞれ違う要素だが同じ処理で反映させる事が出来る。
option.data = {"sort":"message.asc", "limit":"100"};
var success = function(response, status){
	this.empty();
	for (i = 0; i < response.length; i++) {
		var msg = $("<div/>").text(response[i].message);
		this.append(msg);
	}
};
$('#messages1')._GET(option, success, error);
$('#messages2')._GET(option, success, error);

最終案のソースを見てjqueryを知っている人ならピン!ときた人もいるかと思います。要はjquery.simplejsonrest.jsを使うとフォーム値に関してはリクエストするのに基本コーディングが無くなります。submit感覚で使えちゃうって事。