prototype.js 1.6 ããªãªã¼ã¹ããã¾ããããä»æ¥ã¯ãAjax ã¯ã©ã¹ã®ã話ã§ãã
prototype.js ãå©ç¨ããªãå ´å
prototype.js ãå©ç¨ããªãã§ãXMLHttpRequest ãªãã¸ã§ã¯ããéåæã«å©ç¨ããå ´åãå ¸åçã«ã¯ä»¥ä¸ã®ãããªã³ã¼ãã«ãªãã¾ãããã® JavaScript ã®ã³ã¼ããç½®ãã¦ãããµã¼ãããããã¡ã¤ã« foo.txt ãåã£ã¦ãã¦è¡¨ç¤ºãã¾ãã
var request = new XMLHttpRequest(); request.onreadystatechange = function() { if (request.readyState == 4) { alert(request.responseText); } } request.open('GET', 'foo.txt', true); request.send(null);
readyState ã®æå³ã¯ä»¥ä¸ã®éãã§ãã
0 | open() ã¯ã¾ã å¼ã³åºããã¦ããªã |
1 | open() ã¯å¼ã³åºãããããsend()ã¯å¼ã³åºããã¦ããªã |
2 | send() ã¯å¼ã³åºãããããã¾ã ãµã¼ãã¯å¿çãã¦ãªã |
3 | ãµã¼ããããã¼ã¿ãåä¿¡ä¸ã§ãã |
4 | ãã¼ã¿ã®åå¾ãå®äºãã |
é常ã®ããã°ã©ã ã§ã¯ãåå¾ãå®äºãããã¼ã¿ãæ±ãã¾ããããreadyState ã 4 ã®ã¨ãã®ã³ã¼ããæ¸ããã¨ã«ãªãã¾ãã
ãããã風㫠XMLHttpRequest ãªãã¸ã§ã¯ããç´æ¥æä½ããã¨ãã®åé¡ç¹ã¯ã以ä¸ã®éãã§ãã
- IE 7 ããåã® IE ã§ã¯ãXMLHttpRequest ã¯ãªããActiveXObject ãå©ç¨ããå¿ è¦ãããã
- readyState ã«è¦ãã«ããæ°åã使ããªããã°ãªããªã
prototype.js ã® Ajax ãªãã¸ã§ã¯ã
ä¸è¨ã®åé¡ã¯ãprototype.js ã® Ajax.Request ã¯ã©ã¹ã使ãã¨è§£æ±ºã§ãã¾ããä¸è¨ã®ã³ã¼ãã¯ã以ä¸ã®ããã«æ¸ãã¾ãã
new Ajax.Request('foo.txt', { method: 'get', onSuccess: function(transport) { alert(transport.responseText); } });
ã¯ãã¹ã»ãã©ã¦ã¶
XMLHttpRequest ã®åé¡ã¯ãAjax.getTransport ã§è§£æ±ºããã¦ãã¾ããã©ã®é¢æ°ãå®è£ ããã¦ãã¦ãããããã«ãé çªã«è©¦ãã¦ãã£ã¦ãã訳ã§ãã
var Ajax = { getTransport: function() { return Try.these( function() {return new XMLHttpRequest()}, function() {return new ActiveXObject('Msxml2.XMLHTTP')}, function() {return new ActiveXObject('Microsoft.XMLHTTP')} ) || false; }, activeRequestCount: 0 };
æ°åã®æååå
æ°åã¯ä»¥ä¸ã®ããã«æååã«ç½®ãæãããã¦ãã¾ãã
Ajax.Request.Events = ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
ãããã®æååã¯ãå©ç¨ããéã«å é ã« 'on' ãä»ãããã¾ããä¸è¨ã®è¡¨ã¨ãã¼ã¸ããã¨ããããªãã¾ãã
0 | onUninitialized | open() ã¯ã¾ã å¼ã³åºããã¦ããªã |
1 | onLoading | open() ã¯å¼ã³åºãããããsend()ã¯å¼ã³åºããã¦ããªã |
2 | onLoaded | send() ã¯å¼ã³åºãããããã¾ã ãµã¼ãã¯å¿çãã¦ãªã |
3 | onInteractive | ãµã¼ããããã¼ã¿ãåä¿¡ä¸ã§ãã |
4 | onComplete | ãã¼ã¿ã®åå¾ãå®äºãã |
ããã©ã人ãªããæ°ã¥ãã§ãããããreadyState 4 㯠onSuccess ã§ã¯ãªããonComplete ã¨å®ç¾©ãã¦ããã¾ããä¸ä½ã©ããããã¨ã§ããããï¼ ä»æ¥ã®æ¬é¡ã¯ããã®è¬ãæ¢ããã¨ã§ãã
Ajax.Request.respondToReadyState
Success ãªã©ã®ãã¼ã¯ã¼ãã§ãAjax.Request ã¯ã©ã¹ã®ä¸ãæ¤ç´¢ãã¦ã¿ãã¨ãrespondToReadyState ãè¦ã¤ããã¾ãããããèªãã§ããã¾ãããã
Ajax.Request = Class.create(Ajax.Base, { ... respondToReadyState: function(readyState) { var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this); if (state == 'Complete') { try { this._complete = true; (this.options['on' + response.status] || this.options['on' + (this.success() ? 'Success' : 'Failure')] || Prototype.emptyFunction)(response, response.headerJSON); } catch (e) { this.dispatchException(e); } var contentType = response.getHeader('Content-type'); if (this.options.evalJS == 'force' || (this.options.evalJS && contentType && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i))) this.evalResponse(); } try { (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON); Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON); } catch (e) { this.dispatchException(e); } if (state == 'Complete') { // avoid memory leak in MSIE: clean up this.transport.onreadystatechange = Prototype.emptyFunction; } }, ... };
state 㨠response
ã¾ãå¤æ°ã§ãã
var state = Ajax.Request.Events[readyState]; var response = new Ajax.Response(this);
state ã«ã¯ readyState ãæååã«ç´ãããã®ãå ¥ãã¾ããresponse ã«ã¯ããµã¼ãã®å¿çã Ajax.Response ã¯ã©ã¹ã§æ½è±¡åãããªãã¸ã§ã¯ããå ¥ãã¾ãã詳細ã¯ç¥ãå¿ è¦ã¯ããã¾ããã
Complete state (1)
state ã 'Complete' ã®ã¨ããã¾ã以ä¸ã®ã³ã¼ããå¼ã°ãã¾ãã
(this.options['on' + response.status] || this.options['on' + (this.success() ? 'Success' : 'Failure')] || Prototype.emptyFunction)(response, response.headerJSON);
ããã¯ãé¢æ°å¼ã³åºããªã®ã§ãããåãã«ããã®ã§è£å©å¤æ°ã使ã£ã¦æ¸ãæãã¦ã¿ã¾ãããã
var successOrFailure = this.success() ? 'Success' : 'Failure'; var func = this.options['on' + response.status] || this.options['on' + successOrFailure] || Prototype.emptyFunction; func(response, response.headerJSON);
response.status 㯠'200' ã®ãã㪠HTTP ã®å¿çã³ã¼ãã§ãããªã®ã§ã'on200' ã®ãããªé¢æ°ãå®ç¾©ããã¦ãããããã®é¢æ°ãæ¡ç¨ããã¾ãã
é¢æ° success ã®å®ç¾©ã¯ä»¥ä¸ã®éãã§ãã
success: function() { var status = this.getStatus(); return !status || (status >= 200 && status < 300); },
ãã£ã¦ãHTTP ã®å¿çã³ã¼ããã200 以ä¸ã§ãã㤠300 ããå°ãããã°ã'Success' ã«ãªãã¾ããããã§ãªããã°ã'Failure' ã«ãªãã¾ããããããã®å ´åã«ãããããã«å¯¾å¿ãã 'onSuccess'/'onFailure' é¢æ°ãå®ç¾©ããã¦ããã°ããã®é¢æ°ãæ¡ç¨ããã¾ãã
'on200' ãªã©ã 'onSuccess'/'onFailure' ãå®ç¾©ãããªãå ´åãä½ãããªãé¢æ°ãæ¡ç¨ããã¾ãã
æ¡ç¨ãããé¢æ°ããresponse 㨠response.headerJSON ãå¼æ°ã¨ãã¦å®è¡ãããã®ã§ãã
ãã®ããã«ãã¦ãããã§ã¯ onComplete ã§ã¯ãªã onSuccess ãå®è¡ãããããã§ãã
Complete state (2)
state ã Complete ã®ã¨ããããã«ä»¥ä¸ã®ã³ã¼ããå®è¡ããã¾ãã
var contentType = response.getHeader('Content-type'); if (this.options.evalJS == 'force' || (this.options.evalJS && contentType && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i))) this.evalResponse();
ããªãã¡ãevalJS ãªãã·ã§ã³ã 'force' ã«ãã¦ããããevalJS ã true (ããã©ã«ã)ãã¤ãContent-Type: ã text/javascript ãªã©ã§ããã°ããµã¼ãããåãåã£ããã¼ã¿ã JavaScript ã®ã³ã¼ãã ã¨æã£ã¦å®è¡ãã¾ãã
æå¾ã®é¨å
次ã«ãstate ã®å¤ããªãã§ãããã¨ã以ä¸ã®ã³ã¼ããééãã¾ãã
(this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
ããã§ã'onLoaded' ã 'onComplete' ãå®ç¾©ããã¦ããã°å®è¡ããããã¨ã«ãªãã¾ãã'onSuccess' 㨠'onComplete' ã®ä¸¡æ¹ãå®ç¾©ããã¦ããããµã¼ãã¨ã®éä¿¡ãæåããå ´åã両æ¹ã®é¢æ°ãå®è¡ããããã¨ã«æ³¨æãã¾ãããã
ãã®ä»
state ã¯å¥ã«ãXMLHttpRequest ãªãã¸ã§ã¯ããªã©ãä½ãããã¨ãã«å¼ã°ãã 'onCreate' ã¨ããé¢æ°ãå®ç¾©ã§ãã¾ãã
ã¾ã¨ã
- | onCreate | Ajaxãªãã¸ã§ã¯ããä½ããã |
0 | onUninitialized | open() ã¯ã¾ã å¼ã³åºããã¦ããªã |
1 | onLoading | open() ã¯å¼ã³åºãããããsend()ã¯å¼ã³åºããã¦ããªã |
2 | onLoaded | send() ã¯å¼ã³åºãããããã¾ã ãµã¼ãã¯å¿çãã¦ãªã |
3 | onInteractive | ãµã¼ããããã¼ã¿ãåä¿¡ä¸ã§ãã |
4 | onComplete | ãã¼ã¿ã®åå¾ãå®äºãã |
- | onSuccess | ãã¼ã¿ã®åå¾ãæåãã(200çªå°) |
- | onFailure | ãã¼ã¿ã®åå¾ã失æãã |
- | onXYZ | ãã¼ã¿ã®åå¾ã®çµæã XYZ ã§ãã |