prototype.js ã§ç°¡å㪠Ajax ã®ãµã³ãã«
é©å½ãªææ³ãè¨ãã ããªã®ããªããªã®ã§ä¾ãæ¸ãã¾ãããããä½ããhigeponã«ããããããããããããã
いまさら人に聞けないAjaxと簡単なサンプル - higepon blog
ã»ç»é¢ä¸ããã¹ãã©ãã«ã«è¦ããé¨åããã¦ã¹ã¯ãªãã¯ããã¨ç·¨éå¯è½ã«ãªã
ã»æååãå ¥åããEnterãã¼ãæ¼ãã¨ãããã¹ããããã¯ã°ã©ã¦ã³ãã§æ´æ°ãããã©ãã«è¡¨ç¤ºã«æ»ãã
ã»ç»é¢ã¯ãªãã¼ããããªãã
å®æåã¯ããã
http://childtv.org/prototype/sample.html
使ã£ã¦ããã®ã¯ Prototype 1.3.1 ã§ãã
http://prototype.conio.net/dist/prototype-1.3.1.js
sample.html
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja"> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <script type="text/javascript" src="prototype.js"></script> <script type="text/javascript" src="EditableTextBox.js"></script> <script type="text/javascript"> Event.observe(window, 'load', function() { var label = new EditableTextBox($('label')); label.onSubmit = function() { new Ajax.Request('/hatenak/keyword', { method: 'get', parameters: 'word=' + encodeURIComponent(label.getLabel()), onSuccess: function(transport) { alert(transport.responseText); } }); }; }, false); </script> <title>prototype.js 㧠簡å㪠Ajax ã®ãµã³ãã«</title> </head> <body> <div id="label">ããã©ã«ãã®å¤</div> </body> </html>
EditableTextBox.js
var EditableTextBox = Class.create(); EditableTextBox.prototype = { // åæå // @param label ããã¹ãã¾ãã¯ãããã¯ã¨ã¬ã¡ã³ã initialize: function(label) { // ããã¹ãã©ãã«ã®ã¨ã¬ã¡ã³ã this.text = document.createElement('span'); Event.observe(this.text, 'click', this.edit.bindAsEventListener(this), false); // ç·¨éãã¦ãæã®ã¨ã¬ã¡ã³ã this.form = document.createElement('form'); this.input = document.createElement('input'); this.input.type = 'text'; this.form.appendChild(this.input); Event.observe(this.form, 'submit', this.submit.bindAsEventListener(this), false); this.setLabel*1; if(this.onSubmit) this.onSubmit.call(this, event); Event.stop(event); } };
大ã¾ããªè§£èª¬ãEditableTextBox.js ã§ç·¨éå¯è½ãªããã¹ãã©ãã«ã®ã³ã³ãã¼ãã³ããå®ç¾©ãã¦ãã¾ããç·¨éã確å®ãã㨠onSubmit ãå¼ã°ããã®ã§ãããã§è¡ã Ajax ãªå¦çã sample.html ã«æ¸ãã¦ããã¾ããã®ããããªã higepon ããã£ã¦ãã¿ããã«ãã¿ã£ã¨æ¸ããã»ããæ©ããã ãã©ã使ãæ¹ã解説ããã«ã¯ãã£ã¡ã®ãããããªãã¨ãããã¨ã§ã
ã§ã¯ã¾ãçã sample.html ããè¦ã¦ããã¾ããããhead å
㧠prototype.js 㨠EditableTextBox.js ãèªãã§ãã
<script type="text/javascript" src="prototype.js"></script> <script type="text/javascript" src="EditableTextBox.js"></script>
æ¬é¡ã¯æ¬¡ã® script ã¿ã°ã®ä¸èº«ãHTML ã®ä¸èº«ãèªã¿çµãããªããã¡ã«å®è¡ãããã¨å°ãã®ã§ãprototype.js ãæä¾ãã Event.observe ã使ã£ã¦ãã¼ãæã«åæåãå®è¡ãããããã«ãã¦ãã¾ããEvent.observe ã®ä½¿ãæ¹ã¯ addEventListener ã¨åãã§ãã
Event.observe(element, type, observer, useCapture)
åæåã¯ãã¾ã EditableTextBox ãä½ã£ã¦ããããã® onSubmit ãã»ãããããã§ã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ããªã«ããè¦æ £ãã¬é¢¨ã ã¨æãã®ã§ãã
var label = new EditableTextBox($('label'));
ããã¯é¢æ°åã '$' ã§ããé¢æ°ã§ãæååã渡ããã㨠getElementById ã§ã¨ã¬ã¡ã³ãããã¨ã¬ã¡ã³ãã渡ãããã¨ã¨ã¬ã¡ã³ããã®ã¾ã¾ãè¿ãã¾ããããã使ã㨠id åã¨è¦ç´ ãã·ã¼ã ã¬ã¹ã«æ±ããããã§ããããã§ã¯ body å ã«ãã div#label ãã³ã³ã¹ãã©ã¯ã¿ã«æ¸¡ãã¦ãã¾ããããã¦ã¿ããªå¤§å¥½ã Ajax ã onSubmit ã®ä¸ã§ã§ã¦ããã®ã§ãããããã¯ããç°¡åã
new Ajax.Request('/hatenak/keyword', { method: 'get', parameters: 'word=' + encodeURIComponent(label.getLabel()), onSuccess: function(transport) { alert(transport.responseText); } })
URL 㨠method ããã³ãã©ã Ajax.Request ã«ä¸ããã ãã'get' ã 'post' ã«ããã°ãã®ã¾ã¾ POST ãã§ãã¾ãããã³ãã©ã¯ readyState ã«åãã㦠onUninitialized, onLoading, onLoaded, onInteractive, onComplete ã¨ãå®äºããæã« on + (ã¹ãã¼ã¿ã¹ã³ã¼ã) ã onSuccess, onFailure ãå¼ã°ãããã¡ããã¨éä¿¡ã§ããæã ããæ±ããªãããã¿ããã« onSuccess ãæ¸ãã¦ç½®ãã°ãããå¼æ°ã® transport 㯠XMLHttpRequest ãªãã¸ã§ã¯ãã§ãããã㧠sample.html ã¯çµããã
EditableTextBox ãæ¸ãã¦ãã EditableTextBox.js ã®èª¬æã«ç§»ãã¾ãããã®ã³ã³ãã¼ãã³ãã¯
- åãªãã©ãã«
- ç·¨é
ã®2ã¤ã®ç¶æ ãæã£ã¦ãã¦ãããããã«å¯¾å¿ããã¨ã¬ã¡ã³ã text 㨠form ãã¨ã£ããã²ã£ãã表示ãã¾ããã§ã¯ãã£ããã³ã¼ããè¦ã¦ã¿ã¾ãããã
var EditableTextBox = Class.create();
ããã¯ååãæ¸ããããã«
function EditableTextBox() { this.initialize(arguments); }
ã表ãã¦ãã¾ããinitialize ã®ä¸ã§ã¯åã¨ã¬ã¡ã³ããä½æãã¾ããã©ãã«ã®ã¨ã¬ã¡ã³ããä½ãæ§åãè¦ã¦ã¿ã¾ãããã
// ããã¹ãã©ãã«ã®ã¨ã¬ã¡ã³ã this.text = document.createElement('span'); Event.observe(this.text, 'click', this.edit.bindAsEventListener(this), false);
Event.observe ã¯ãã£ãè¦ãã¨ããã§ãããã§ã¯ã©ãã«ãã¯ãªãã¯ãããæã«ç·¨éã¢ã¼ãã«ç§»ãããæ示ãã¦ãã¾ããåé¡ã¯
this.edit.bindAsEventListener(this)
ã£ã¦ã®ã¯ä½ãã¨ãããã¨ã§ããthis.edit ã¯ããã®ä¸ã§å®ç¾©ãã¦ãããã©ãã«ã¨ç·¨éã®ã¨ã¬ã¡ã³ããå ¥ãæ¿ããã¡ã½ããã
edit: function(event) { this.element.replaceChild(this.form, this.text); Field.activate(this.input); // ãã©ã¼ã«ã¹ã»é¸æ },
JavaScript ã§ã¯ãªã¹ãã¼ã¨ãã¦åãªãé¢æ°ã渡ãã®ã§ãOO ãªæãã§æ¸ãã¦ããå ´åãã®é¢æ°ãã©ã®ãªãã¸ã§ã¯ãã«å¯¾ãã¦å¼ã³åºãããããåé¡ã«ãªãã¾ããä¾ãã° bindããã¡ããã¨æ¸ããã«
Event.observe(this.text, 'click', this.edit, false);
ã«ããã¨ãã¤ãã³ããèµ·ããã¨ãé¢æ° this.edit 㯠span ã¨ã¬ã¡ã³ãã§ãã this.text ã«å¯¾ãã¦å¼ã³åºããã¦ãããããã¨é¢æ°å ã® this ãæ³å®å¤ãªã®ã§ã¨ã©ã¼ã«ãªãã¾ããã¡ããã¨ç¾å¨ã® EditableTextBox ãªãã¸ã§ã¯ãã«å¯¾ãã¦é¢æ°ãå¼ã³åºãããããã«ããã«ã¯ prototype.js ã«æ¸ãã¦ããããã使ãã¨ããã
Function.prototype.bind = function(object) { var __method = this; return function() { __method.apply(object, arguments); } }
Function ãæ¡å¼µãã¦ãä¸ãããããªãã¸ã§ã¯ãã«å¯¾ãã¦èªèº«ãå¼ã³åºãããé¢æ°ãä½ã bind ã¡ã½ãããä½ã£ã¦ãã¾ãã
var func = this.edit.bind(this); func();
ãããã㨠func() 㯠this.edit() ã¨åãã«ãªãããã㧠this ãã©ã£ããã£ã¡ããåé¡ã¯è§£æ±ºãã ãã©ã¤ãã³ãã®ãªãã¶ã¼ãã¼ã¨ãã¦é¢æ°ã渡ãå ´åã«ã¯ããä¸æ©ä¾¿å©ãªãã®ãããã®ã§ãã
Function.prototype.bindAsEventListener = function(object) { var __method = this; return function(event) { __method.call(object, event || window.event); } }
W3C ã®ã¤ãã³ãã¢ãã«ã§ã¯ãªãã¶ã¼ãã¼ã«å¼æ°ã¨ãã¦æ¸¡ãããã¤ãã³ããªãã¸ã§ã¯ãããè²ã æ å ±ãå¾ãããããã ãã©ãIE ã§ã¯å¼æ°ã®ä»£ããã«ã°ãã¼ãã«å¤æ° window.event ã使ããããã®å·®ãåãã¦ãããããã§ããããã使ã£ãã®ããã£ãã®ãã¤ã
Event.observe(this.text, 'click', this.edit.bindAsEventListener(this), false);
ããå ¨é¨ããã£ãããããã£ãããæ®ãã¯å°ãé¨åã¯ãªãã¨æãã¾ãã$F ã Form.Element.getValue ã®ç¥è¨ã§ãã㯠form ã®è¦ç´ ã®å¤ãå¾ãé¢æ°ã ã¨ããEvent.stop ã preventDefault 㨠stopPropagation ã ã¨ã㯠prototype.js ãæ¤ç´¢ãã¦ãã ããããããã
*1:typeof label == 'string') ? label : label.innerHTML); // EditableTextBox å ¨ä½ã®ã¨ã¬ã¡ã³ã if(typeof label == 'string') { this.element = document.createElement('div'); } else { this.element = label; this.element.innerHTML = ''; } this.element.appendChild(this.text); }, // æ¸ãã¦ããæåã®ã¢ã¯ã»ãµ getLabel: function() { return $F(this.input); // return this.input.value ã¨åã }, setLabel: function(label) { this.text.innerHTML = label; this.input.value = label; }, // ç·¨ééå§ edit: function(event) { this.element.replaceChild(this.form, this.text); Field.activate(this.input); // ãã©ã¼ã«ã¹ã»é¸æ }, // ç·¨éçµãã // ããããã° this.onSubmit ãå¼ã¶ submit: function(event) { this.element.replaceChild(this.text, this.form); this.setLabel($F(this.input