Maintainable JavaScriptã«ã¿ããã³ã³ããã¹ãã¨ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ã®åé¢
å人çãªãã¨
èªæ¸ã¯ããããèªå·±æè³ï¼ã«ããããã®ãªã®ã§ã±ãããããããªãããªããã¨ã¯æãã¤ã¤ãå¯è½ãªãå®ãå¤æ®µã§ãã大ããªãªã¿ã¼ã³ãå¾ããããã¼ãã¨ãããã¨ã§æè¿ã¯Oreillyã®åé¡ã»ã¼ã«ã«ç®ãå ãããããã«ãªãã¾ãããè±èªã¯ãæ¾ãèªã¿ãããè¾ãã¨ããæ¬ ç¹ãããã®ã§ãããã¾ããå®ãããå ¨ã¦ã®æ¬ã«ã¡ããã¨è¨³ãåºãããã§ããªãã®ã§ãããããªãã¨ã
ãããªãã㧠"Maintainable JavaScript" ã¨ããæ¬ãèªãã§ããã®ã§ããããã®ä¸ã®Event Handlingã«ã¤ãã¦ã®ç« ããããããããããã¨ããæãã ã£ãã®ã§ã¡ãã£ã¨ç´¹ä»ã
Maintainable Event Handling
jQueryè¦ãããï¼ã£ã¦æãã®äººãã¨ããããã³ã¼ããæ¸ãã¨ãã ããããããªæãã«ãªãã¾ãããã¡ãªã¿ã«ãããã¯å¥ã«jQueryãã©ãã¨ããã話ã§ã¯ãªãã¦ãæ¬è³ªçã«ã¯çDOMã§ãä»ã®ã©ã¤ãã©ãªã§ãåããã¨ã§ãã
$(function() { $("#foo").on("click", function(e) { // ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯1 // ... ããã¾ã§é·ãã¯ãªã ... }); $("#bar").on("click", function(e) { // ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯2 // ... ã¾ãã¾ãé·ã ... }); $("#baz").on("click", function(e) { // ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯3 // ... ããã¾ã§é·ãã¯ãªã ... }); })();
è¦ã¯ãç¹å®ã®Elementã«ã¤ãã³ããã³ãã©ããã£ã¤ãã¦ãã¤ãã³ããã³ãã©ã®ä¸ã«ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ããã©ãã©ã£ã¨æ¸ãããã§ããã
ã¡ãªã¿ã«ããããããã¡ï¼ãã£ã¦è¨ãããããã§ã¯ãªãã§ããã³ã¼ãã®è¦æ¨¡ã«å¯¾ãã¦é©åãªæ½è±¡åã®ã¬ãã«ã£ã¦ããã¨æãã®ã§ãéçãªWebãã¼ã¸ã«é£¾ãä»ããããã ããªãããããæ¸ãæ¹ã§ãããã¾ã§æ³£ããã¿ããã¨ã¯ãªãã¨æãã¾ããå°è¦æ¨¡ãªã³ã¼ãããæ¸ãäºå®ããªãã®ã«å¤ã«ã¯ã©ã¹ãã¼ã¹ãªãªãã¸ã§ã¯ãæåãé å¼µã£ã¦ã¿ããã¨ããæ確ãªæå³ãªãã«ãä»ã¯Client Side MVCãæµè¡ããªãã ããï¼ãã¨ããã£ã¦ãã¾ãã¨éã«å¾ã§èªåãããããã¨ã«ãªãã§ãããã
ã§ã"Maintainable JavaScript"ã®èè ã§ããNicholas Zakaså çã¯ãããããã¤ãã³ããã³ãã©ã®ä¸ã«ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ã®æ¸ãã¦ããã³ã¼ãã¯ãããªãï¼ãã¨è¨ãã¾ãããªããã¨ããã°ã
- å®è£ ããã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ããã¤ãã³ããã³ãã©æå¤ã®å ´æããå¼ã³åºããããªãã®ã¯ã¾ã¾ãããã¨ã
- ä¾ãã°ãã¦ã¼ã¶ã®ã¢ã¯ã·ã§ã³èµ·å ãããªãã¦Viewãããã°ã©ã ããæ´æ°ãããæã¨ã...ï¼ããããï¼
ãªã®ã§ãã¤ãã³ããã³ãã©ã¨ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ã¯åé¢ãã¦ããã¨ãæªæ¥ã®èªåã«æè¬ããã¾ãã
$(function() { var Application = { handleClick: function(e) { this.showPopup(e); }, showPopup: function(e) { // ã¢ããªã±ã¼ã·ã§ã³ãã¸ã㯠} }; $("#foo").on("click", function(e) { Application.handleClick(e); }); })();
Zakaså
çã¯ãããã§ãã¾ã ãããï¼ãã¨è¨ãã¾ãããªããªããä¸ã®ä¾ã®showPopupã¨ããé¢æ°ãevent objectãå¼æ°ã«åã£ã¦ãã¾ã£ã¦ããã®ã§ããã®ã¾ã¾ã§ã¯çµå±ä»ã®æããå¼ã¹ãªãããã§ããã
ã¾ã ããã¦ã¼ã¶ã®ã¯ãªãã¯ãã¨ããå¼ã³åºãã³ã³ããã¹ãã¨ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ãçµåãã¦ãã¾ã£ã¦ãã¾ãã
ãªã®ã§ã以ä¸ã®ããã«å¤æ´ãã¾ãã
$(function() { var Application = { handleClick: function(e) { this.showPopup(e.clientX, e.clientY); // jQueryã®Event Objectã£ã¦ããã¤ãçãã¦ãã£ãï¼ã¾ãããã }, showPopup: function(x, y) { // ã¢ããªã±ã¼ã·ã§ã³ãã¸ã㯠} }; $("#foo").on("click", function(e) { Application.handleClick(e); }); })();
ããããã¨ãã¦ã¼ã¶ã®ã¯ãªãã¯ä»¥å¤ã®ã¿ã¤ãã³ã°ã§showPopupããä»»æã®åº§æ¨x, yãå¼æ°ã¨ãã¦å¼ã³åºããããã«ãªãã¾ããã
ã¾ããshowPopupã®ãã¹ããæ¸ãããããªãã¨æãã¾ãããã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ã«ã¯ç´æ¥Eventã渡ãããå¿
è¦ãªãã¼ã¿ã®ã¿ã渡ããã¨ãããã¨ã§ããã
ã¨ããã§ããã¡ãã¡ã¤ãã³ãã®ç¨®é¡ãã¨ã«handleãªãã¨ãã¨ããé¢æ°ãç¨æããã®ãã¢ã¬ãªã®ã§ãhandleEventã§ã¾ã¨ãã¦ãã¾ãã®ãå ´åã«ãã£ã¦ã¯ã¢ãªããããã¾ããã
$(function() { var Application = { handleEvent: function(e) { switch (e.type) { case "click": this.showPopup(e.clientX, e.clientY); break; case "scroll": ... case "drag": ... } }, showPopup: function(x, y) { // ã¢ããªã±ã¼ã·ã§ã³ãã¸ã㯠} }; $("#foo").on("click", Application); })();
jQueryãæ£ããæ±ã£ã¦ããããã©ããã¾ã§ã¯ç¥ãã¾ããããhandleEventã¨ããé¢æ°ãçãããªãã¸ã§ã¯ããã¤ãã³ããã³ãã©ã¨ãã¦æå®ã§ããã®ã¯DOM Level2ã®ãã£ãã¨ããä»æ§ã§ãã(http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventListener)
ããã¾ã§æ¥ãã°ãããã«åå©ç¨æ§ãé«ããããã®æ½è±¡åãè¡ãã¾ã§ã¯å¾ä¸æ©ã§ãã
(function() { function ModalController(el) { this.elem = el; el.on("click", this); } ModalController.prototype.handleEvent = function(e) { switch (e.type) { case "click": this.showPopup(e.clientX, e.clientY); break; case "scroll": ... case "drag": ... } }; ModalController.prototype.showPopup = function(x, y) { // ã¢ããªã±ã¼ã·ã§ã³ãã¸ã㯠}; })();
$(function() { var controller = new ModalController($("#foo")); })();
ããç¾å®ã«ãããªãã¨ãããå§ããå ´åã¯ãä½ããã®æ¢æã®ãã¬ã¼ã ã¯ã¼ã¯ã«ä¹ã£ãããã¨ãèãå§ããæ¹ãããã¨æãã¾ãã
å¼ã³åºãã³ã³ããã¹ãã¨ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ã®åé¢ãã«ã¤ãã¦
ä¸è¨ã®è©±ã®ãã¢ã¯ããã¦ã¼ã¶ã®ã¯ãªãã¯ãè¡ãããããã«ä¼´ãEvent Objectããããã¨ããã³ã³ããã¹ãã¨ãå®éã®ã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ãåé¢ãããã¨ã§åå©ç¨æ§ãé«ãã¦ããé¨åã«ããã¾ãã
ããã£ã¦ã©ããã§èãã話ã§ãããWebã¢ããªã±ã¼ã·ã§ã³ãã¬ã¼ã ã¯ã¼ã¯ã®å¤ããæ¡ç¨ãã¦ãããMVCãã¿ã¼ã³ã«åã£ã¦ã¢ããªãä½ãå ´åã§ããControllerã«ãããHTTP Requestã«ã²ãã¥ãã³ã³ããã¹ãï¼ä¾ãã°Context Objectã§ãã£ãããHTTP Requestã表ç¾ããObjectã§ãã£ãããFormã表ç¾ããObjectã§ãã£ããï¼ããã¸ãã¹ãã¸ãã¯ã®ã¬ã¤ã¤ã«æã¡è¾¼ã¾ãªããä¼æããªããã¨ããæ¸ãæ¹ããã¾ã¨ããªäººãªãããã¨æãã¾ãããããã¦ãããªãã¨ãåããã¸ãã¹ãã¸ãã¯ãbatchãworkerãªã©ã®ä»ã®å ´æããåå©ç¨ã§ããªããªã£ã¦ãã¾ãã¾ãããããMVCã¨ãããã¬ã¼ã ã¯ã¼ã¯/èªå½ã使ããªãã¦ãããã®è¾ºã®ååã¯ã¾ãå¤ãããªãããããªãããªã¼ã¨æãã®ã§ãããå çã©ããªãã§ãããã
iOSãAndroidã®ãã¤ãã£ãã¢ããªã§ãããã¯ãã¦ã¼ã¶ã®ã¢ã¯ã·ã§ã³ã«ç´ä»ããã³ã³ããã¹ãã¨ãã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ã¯åé¢ããã¨ããããã§ãããJavaScriptã§ã®Event Handlingã«éãããã¢ããªã±ã¼ã·ã§ã³ãã¸ãã¯ã¯è¤æ°ã®å ´æ/ã³ã³ããã¹ãããå¼ã³åºãããåæã§æ¸ããã®ã ãï¼ã¨ããèãæ¹ã¯ã³ã¼ãã®åå©ç¨æ§ãé«ãã¦ãããã®ã§ã¯ãªãããªã¼ã¨æãã¾ãã