JavaScriptã§ã®ãã¹ããéçºã«ã¤ãã¦ã®ã¢ã¦ãããã
æè¿JavaScriptãå人çã«åå¼·ãã¦ãããã§ããããããªJSåå¿è ãªããè²ã 試ããªãã§æ°ãä»ããéçºã¨ãTDDã¨ãã«ã¤ãã¦è²ã æãã¨ãããã¢ã¦ãããããã¦ã¿ãããã¨æãã¾ãã
ä¸çªå¤ãã®ã¯ãClientSideJSã§ã使ã£ã¦ãã®ã¯jQueryã¨Qunitãä¸å¿ã§ããã
ã§ãããããã¯å¥ã®ãã¬ã¼ã ã¯ã¼ã¯ããServerSideJSãªãããåºã¦ãã¾ããã
ä»èªãã§ãæ¬ãçµãã£ããããã£ã¡ã®æ¬ãè¦ã¦ã¿ããã¨æã£ã¦ããã®ã§ã
Test-Driven JavaScript Development: Safari Books Online
ãã®åã«ãããæ¸ãã¦ãããã¨ããç®çã§ããèªåã«ä»ããä¸ã¤ã®Tagã¨ããæãã§ãã
ãã¾ãä¸è²«æ§ã«æãããåãæµãããã¨æãã¾ãã
Ajax 㨠API
以åこんな記事ãæ¸ããããã«ããµã¼ãå´ãAPIã§ãã¼ã¿ãæä¾ãããã¸ãã¯ãã¯ã©ã¤ã¢ã³ãå´ã«åºããã¿ã¤ãã®éçºæ¹æ³ãè²ã
試ãã¦ã¾ãã
ä¸è½ã§ã¯æãã¾ãããã以ä¸ã®ãããªç¹å¾´ãæãã¾ãã
- APIã®ã¿ãã¤ã³ã¿ãã§ã¼ã¹ã«ãã¦ããããççµåãä¿ã¡ãããã
- çHTMLããããã®ã§ãã³ãã¬ã¼ãã¨ã³ã¸ã³ã®æ°æã¡æªãè¨æ³ãåºã¦ããªã(JSPã¨ãè¦æã§ããã)
- ãã¼ã¿ããªãã£ãé«ãã(ãã³ãã¬ã¨ã³ã¸ã³ä½¿ã£ã¦ãªããããµã¼ãå´ã¯APIãåããªãã©ããªè¨èªãã©ããªãã©ãããã©ã¼ã ã§ãè¯ã)
éã«ãã©ã³ã¶ã¯ã·ã§ã³ã¨ãèªè¨¼ã»ã»ãã¥ãªãã£å¨ããçç£æ§çèããã¹ãã¨ããã¯å¤ããã§ãããããã¯ã¾ãå¥ã§èãã¾ãã
ããããã¢ããªã¯ã¯ã©ã¤ã¢ã³ãå´ã§å¤ãã®ãã¸ãã¯ã¨è¡¨ç¤ºãããã¦Ajaxã«ãããã¼ã¿ã®ããåããå®è£
ãããã¨ã«ãªãã¾ãã
ããããTDDã§ãããã¨ããã¨ãããã¾ã§ä»ã®è¨èª(Server Side (Java|Python)ã¨ã)ã§ãã£ã¦æ¥ããã¨ã¨ã¯å°ãéãèãæ¹ãå¿
è¦ã«ãªãé¨åãå¤ãæ°ããã¾ãã
DOMå¨ãã¨è¡¨ç¤ºã®ãã¹ã
ã¯ã©ã¤ã¢ã³ããµã¤ãã§ããæç¹ã§ã表示ãæä½ãããã¨ã¯é常ã«å¤ããªãã¾ãããçµè«ã¨ãã¦ã¯ãã表示ã¯åãã¦ããã¹ãã¯ããªãããç¾å®è§£ã®ããã§ãã
ãã¨ãã°ãããªé¢æ°ããã£ãã¨ãã¾ãã
$('#hoge').bind('click', function(e, data) { //ãã¸ã㯠var textdata = data.hoge; //表示å¦ç $('#area').text = textdata; };
ä½ãèããªãã¨ãããªæãã«ãªã£ã¦ãã¾ãã¾ãããããã¯é常ã«ãã¹ãããã«ããã§ãã
表示ã¯ãã¹ãåºæ¥ãªãã¨ããããã§ã¯ãªããä¾ãã°Seleniumçãç¨ããã°ã表示ã®ãã¹ãã¯å¯è½ã§ãã
ãã§ã¯çµæã¨ãã¦ç»é¢ä¸ã«textdataã表示ããã¦ãããã調ã¹ãããã¨ã¯ã§ãã¾ãã
ããããããã¯ããã§éçºãé§åããããã«ãããã¨ãã¦ããããTDDã®ä¸ã§ã®JSã®UnitTestãã¨ã¯ã¹ã³ã¼ããéãã¨èãã¾ãã
æ¸ãããã®ã¯ä¾ãã°ãæ¸ããJSã®ã¡ã½ãããªãã¯ã©ã¹ãªãã®ãã¹ãã§ãã
ããã¨ãä¸çªç°¡åãªã®ã¯ããã®aã®ä¸ã«ããäºã¤ã®å¦çãããããã¨ã§ãã
ã¤ã¾ãããããªã¤ã¡ã¼ã¸
var logic(data){ return data.hoge; }; var view(textdata){ $('#area').text = textdata; }; $('#hoge').bind('click', function(e, data) { //ãã¸ã㯠var textdata = logic(data); //表示å¦ç view(textdata); };
ãããã¦ããã¸ãã¯ã¨è¡¨ç¤ºã®è²¬åãåæ£ãã¦ããã¹ãã®å¯¾è±¡ãlogicã«çµãã¾ãã
logicã¯outputãreturnãªã®ã§é常ã«ãã¹ããããããã§ãã
ã§ã¯viewã¯ã©ããããã
å¦çã®ä¸ã«è¡¨ç¤ºãåºã¦æ¥ãå ´åããã®åçãªè¡¨ç¤ºâDOMã®æä½ã«ã¤ãã¦ã¯ãèªåã¯jQueryã使ã£ã¦ããã«ä»»ãã¦ãã¾ãã
ããããã¹ãããã«ã¯DOMã®ã¢ãã¯ãä½ãããä½ããã®æ¹æ³ã§ç»é¢ãè¦ãå¿
è¦ãåºãã¨æãã¾ãã
ããããèªåã¯ããã«æéãããã¾ããã
ãã¡ãã¨è¡¨ç¤ºããã¦ãããã©ããã¯ãjQueryã®è²¬åã«ãªãã¾ããããã¦ãjQueryèªä½ã¯jQueryã®éçºè
ã«ãã£ã¦ãã¹ãããã¦ãã¾ãã
ããã§ç¡çç¢çãã¹ããæããã¨ã¯jQueryèªä½ããã¹ããããã¨ãã¦ãããã¨ã«ãªãã¨æãã¾ãã
ã¾ãã表示ã«ã¤ãã¦ã®ãã¹ãã¯ä¸è¬çã«ã³ã¹ããé«ãã§ããã¾ãã表示ã¯UIã®å¤æ´ãããã«åããã®ã§ãä¿å®ã®ã³ã¹ããé«ããªããã¡ã§ãã
ãããé¿ããããã«ãæ¬å½ã«è¡¨ç¤ºããå¦çã ãã並ãã§ãããã¨ã確èªããããç¨åº¦ãã©ã¦ã¶ãã確èªãããããã¾ã§ã«ãã¾ãã
bind('click')ã«ã¤ãã¦ãåãã§ã#hogeãã¯ãªãã¯ããã¨ãã«æ£ããã¤ãã³ããçºç«ãã¦ã³ã¼ã«ããã¯ãå®è¡ããã¦ãããã¯ãjQueryã®è²¬åã§ãã
ã¾ãæ¸ãã¦ãè¯ããã§ããããã©ã¦ã¶ããè¦ãæåã§ãæåã®å
ã¯ç¢ºèªåºæ¥ã¦ãã¾ãã®ã§ããã¾ãæ°ã«ããããªãã§é²ãã¾ãã
ãã ããããã§bindããã¤ãã³ããããã¹ãã³ã¼ãã®ä¸ã§$().trigger('eventname')ã§çºç«ãããã¨ã§ä»»æã®ã¿ã¤ãã³ã°ã§å¼ã¶ã«ã¯æå¹ã ã¨æãã®ã§ãå¦çãã«ã¹ã¿ã ã¤ãã³ãã«åºãããã¨ã§å¾ãããå©ç¹ã¯æãã¨æãã¾ãããããããã®ã«ã¹ã¿ã ã¤ãã³ãã®ä¸ã§è¡¨ç¤ºã¨ãã¸ãã¯ãåããã¦ãããªããèªåã¯ãã¸ãã¯ã ãããã¹ãããã®ã§ãããè¨ãå ´é¢ã«ã¯ã¾ã åºä¼ã£ããã¨ã¯ç¡ãã§ãã
ã«ã¹ã¿ã ã¤ãã³ãã®æå¹å©ç¨ã¯ãèªåã¨ãã¦ã¯ä»å¾ã®èª²é¡ã§ãã
ãããã«ãããä»ããã¹ãããªãã§æ¸ãæ§ã«ããããã«ãã§ããã ãlogicã«å¦çãéãã¦ãããã®ãã¹ãã«éä¸ããã¨ããã®ããä¸çªç¾å®çããªã¨æã£ã¦ãã¾ãã
æçµçã«ã¯ã©ã¤ãã©ãªãã©ãã©ãä½ã£ã¦ããããå¾¹åºçã«ãã¹ãããæãã§ããããã
ããã¦ãlogicã®ä¸ã«DOMãåºã¦ããªããã°ãDOMã®Mockç¡ãã§CIã§åããããããã¨ãã§ãã¾ãã
ã¾ãããã種å½ããåã®ãã¨ã§ã¯ããã¾ãããä»ã¾ã§ãã¹ããæèããªãã§JSãæ¸ãã¦ããèªåã¯ãè¨ãã»ã©ã¡ããã¨åºæ¥ã¦ããªãã®ã§ãç¯æ£ãã¦ãããªãã¨ãããªã大äºãªåºç¤ã ã¨æã£ã¦ãã¾ãã
ãã ããã§è¨ã£ã¦ãã®ã¯TDDã®ç¯å²ã®è©±ãªã®ã§ãçµåãã¹ãçãã£ã¨ä¸ã®ã¬ãã«ã®ãã¹ãã§å質ãæèããå ´åã¯ãç»é¢è¡¨ç¤ºãå«ãããã¹ããæ¸ãå¿
è¦ã¯ããã¨æãã¾ãã
jQuery Object ã®ãã¹ã
DOMã§ã¯ãªãjQueryãªãã¸ã§ã¯ãã¨ãã¦ãªãæ¯è¼ããããå ´åã¯å¤ãã§ãã
ãªãã¸ã§ã¯ãã¨ãã¦å±æ§ãåç
§ããæ¯è¼ã¯ãããªæãã«åºæ¥ã¾ãã
ä¾ãã°ãjQueryãªãã¸ã§ã¯ãã®jqueryã¨selectorã¯ä½¿ããããã§ãã
ããã«ãã£ã¦ãã対象ãjQuery Objectãã©ãã?ãã¨ãã»ã¬ã¯ã¿ããªãã ã£ãããã調ã¹ããã¾ãã
var param = '#template'; var $template = $(param); same($template.jquery, $().jquery); //ãããjQueryãªãã¸ã§ã¯ããã©ãã same($template.selector, param); //#templateãã»ã¬ã¯ããããã®ã«ãªã£ã¦ããã
æ¢ãã¨ãã¦ãããã®ä½ã¾ã§ã«ããæ¹ãããããªã¨ãå人çã«ã¯æã£ã¦ãã¾ãã
Ajax ã®ãã¹ã
AjaxãjQueryã使ã£ã¦ããã¾ããä»ã¾ã§ã®ã¡ãã£ã¨ãããã®ã ã¨ãå§åçã«ãGETãã¦è¡¨ç¤ºããªãã¿ã¼ã³ãå¤ãã£ããã§ããã
ã¢ããªãçµãå ´åã¯ãRESTful APIãå©ããGET-POST-PUT-DELETEå¦çããããã¨ãå¤ããªãã¾ãã
ãã®è¾ºãè¸ã¾ãã¦ã©ãããã®ãããããå®éé£ããã¦è²ã
試è¡é¯èª¤ãã¦ããéä¸ã§ã¯æãã¾ãã
ã¨ããããQunitã§asyncTestã®æ¹æ³ãè¦ããæç¹ã§ãã¾ãä½ã¨ãªããã¹ããæ¸ããã¨ãåºæ¥ãæ§ã«ã¯ãªã£ããã§ããã
è²ã
ã¨ã¤ã¾ãããã¨ãå¤ãã£ãã§ãã
ãã®åå ãèããã¨ããããå
ã»ã©ã¨åã
ããã¹ããããUnit(åä½)ã¯ãªããªã®ããã£ã¦ã¨ãã大äºãªã®ããªã¨ããæ°ããã¦ã¾ãã
ä¾ãã°ãç°¡åã«getJSONã§ä»¥ä¸ã®ãããªå¦çãèãã¾ãã
$.getJSON(baseURI+'/hoge', {"foo":1, "bar":2}, function(res) { // res ã«å¯¾ãã¦ã®ã³ã¼ã«ããã¯å¦ç });
å¤ãã®jQueryã®ãµã³ãã«ã§ã¯å¤§æµãããªæãã«æ¸ãã¦ããã¾ããã
ãããããã¹ãããããªãã¦è¨ã£ã¦ãä¸ç¬æ¢ã¾ã£ã¦ãã¾ãã¾ãã
ã¾ããinputã¨outputãèããã¨ã$.getJSONèªä½ã®inputã¯URLããã©ã¡ã¼ã¿ãã³ã¼ã«ããã¯é¢æ°ã§ãã
ã§ãoutputãæå¾
ããã¨ããããã³ã¼ã«ããã¯ãä½ããããã°ãããoutputã¨æãããã¨ãåºæ¥ãããããã¾ããã
ãã¡ããã³ã¼ã«ããã¯ã®ä¸ã®å¦çã«ãããã¾ãããå ¸åçãªä¾ã§ã¯
- return
- ã¯ã©ã¹ã®ç¶æ æ´æ°
- 表示
- ã¤ãã³ãã®çºç«
ãªãããããã¨æµ®ãã¶ã¨ããã§ãããAjaxãã¼ã¹ã®ã¢ããªã ã¨ãã¬ã¹ãã³ã¹ã®å¤ãå©ç¨ãã¦ããã«å¥ã®Ajaxãªã¯ã¨ã¹ããå¼ã¶ãã¨ãèãããã¾ãã
ãããã«ããããã§ã¯ãä½ããæã¿è¾¼ãä½å°ãå°ãªãã®ã§ãè²ã åãåºãã¦ãã¾ãã¾ãã
var URI = baseURI + '/hoge'; var param = {"foo":1, "bar":2}; var callback = function(res) { // res ã«å¯¾ãã¦ã®ã³ã¼ã«ããã¯å¦ç }; $.getJSON(URI, param, callback);
ããããã¨ãè²ã
ãã¹ããããããªãã¾ãã
ã§ããããä½ããã¹ãããããªãã§ãããä¾ãã°ä»¥ä¸ã®ãããªãã®ãæµ®ãã¶ããããã¾ããã
- ãããããªã¯ã¨ã¹ããã¦å¤ãåå¾ã§ãããã©ããã®ãã¹ãã
- ãã©ã¡ã¼ã¿ã«å¯¾ãã¦ãæå¾ ããã¬ã¹ãã³ã¹ãè¿ã£ã¦ãããã©ããã®ãã¹ãã
- éä¿¡å¾ã®å¦ç(ã³ã¼ã«ããã¯)ãä»æ§éãã«è¡ããããã®ãã¹ã
ä¸ã¤ãã¤ãèªåã®èãã§è©ä¾¡ãã¦ã¿ã¾ãã
ãããããªã¯ã¨ã¹ããã¦ã¬ã¹ãã³ã¹ãåå¾ã§ãããã©ããã®ãã¹ã
ãã®å ´åã¯ãå®éã«ãµã¼ãã«å¯¾ãã¦ãªã¯ã¨ã¹ããæãããããããã¯URLãå·®ãæ¿ãã¦ãã¼ã«ã«ãã¹ã«ç¨æããJSONãã¡ã¤ã«ãªãããå©ããã¨ã§åºæ¥ã¾ãã
é¢åãããã®ã§é°å²æ°ã ã
asyncTest('test request', function(){ var callback = function(res){ ok(res); }; setTimeout(fucntion(){ $.getJSON('./res.json', param, callback); }, 10); });
ã¾ããæ¸ããã ãã§ããã
ã©ããããã¨ãã¨ããã¨ããã㯠$.getJSON()èªä½ããã¹ããã¦ããå¯è½æ§ãããã¨ãããã¨ã§ãã
URIã¨paramãééã£ã¦ããªããã°ããã¼ã¿ã¯åå¾åºæ¥ãã¨ããjQueryã®è²¬åãä¿¡ããã°ããã®ãã¹ãã¯å¿
è¦ç¡ãããã«æãã¾ãã
ãã©ã¡ã¼ã¿ã«å¯¾ãã¦ãæå¾ ããã¬ã¹ãã³ã¹ãè¿ã£ã¦ãããã©ããã®ãã¹ãã
é¢åãªãã§æ¸ãã¾ããããããã¯ãã¡ããæ£ãããã©ã¡ã¼ã¿ã渡ã£ã¦ããã°ããã®å
ã¯ãµã¼ãã®è²¬åã§ãã
APIããã£ããå®ç¾©åºæ¥ã¦ããã°ãURIã¨ãã©ã¡ã¼ã¿ã®çµã¿åããã«å¯¾ãã¦æå¾
ãããã¬ã¹ãã³ã¹ã«ã¤ãã¦ã¯åãã£ã¦ããã¯ãã§ãã
æ¬æ¥ãµã¼ãã®è²¬åãã¯ã©ã¤ã¢ã³ãå´ãããã¹ãããã®ã¯ããããèªä½(APIã®ãã°ãåºãã¨ã)ããç®çã§ç¡ãéãã¯å¿
è¦ãªãã¨èããã¨ãããããããªãã®ããªã¨ã
éä¿¡å¾ã®å¦ç(ã³ã¼ã«ããã¯)ãä»æ§éãã«è¡ããããã®ãã¹ã
å
ã»ã©ã®ä¾ã§ã¯ãAPIã決å®ãã¦ãããªããããã®å ´åã¡ããã¨200ãè¿ã£ã¦ããã?ããããã200ãªãã©ããããï¼ãã404ãªãã©ããããï¼ãã¨ãã£ãåä½ã«çç®ãããã¨ãããã¨ã§ãã
ããèªä½ã¯ã³ã¼ã«ããã¯ãåãåºãã¦ããã°ãããããã®æåã«å¿ããã¬ã¹ãã³ã¹ã®ãã¼ã¿ãã³ã¼ã«ããã¯ã®å¼æ°ã«å·®ãè¾¼ãã°ã§ãã¾ãã
å·®ãè¾¼ãããã«ããããJSONãã¡ã¤ã«ãç¨æãããããªå¿
è¦ãæãã¾ããã
// callbackã¯ä»¥ä¸ã®å®è£ var callback = function(res){ if(res){ //200 return res.msg; }else(res === {}){ //404 return false; } }; test('200',function(){ var res = { 'msg' : 'hello' } same(callback(res), 'hello'); }); test('404',function(){ var res = {} same(callback(res), false); });
ã¹ãã¼ã¿ã¹ã³ã¼ãå¨ããåãã§ãã¬ã¹ãã³ã¹ã®å ´ååããããã³ã¨èãã¦ããã°ãAPIãã®ãã®ããã¹ãããªãã§æ¸ããããããæ¬æ¥æ¸ãã¦ãããããã¹ããªã®ããªã¨æã£ã¦ãã¾ãã
ã¨ã¯è¨ããã®ã®ãã
ãããã¦ããã¨APIã®å¤æ´ã«å¯¾ãã¦ã¡ã³ããã³ã¹ã³ã¹ããä½ãä¿ã¦ãæ°ããã¾ãããã ããã®ã³ã¼ã«æã®URIãparamãæ£ããç©ãã©ããã¯ããã§ã¯è¦ã¦ããªããã¨ã«ãªãã¾ãã
ãã¯ããã1ã¬ã¤ã¤ä¸ã®ãã¹ããããã¯æ¸ããã¨ãå¤ãã£ãããããããããè¨ããã¹ãã¯çµæ§å¤æ´ã«å¼±ãæ°ããã¦ãã¾ãã
ãã®ç¹ã«ã¤ãã¦ã¯ãå°ããã¢ããªãªãããããééã£ã¦ãã¨åããªãã¨ããä¸çªåãããããå½¢ã§ãã£ã¼ãããã¯ãå¾ãããã®ã§ãæ°ãã¤ããã¨ãåºæ¥ãããããã¾ããããè¦æ¨¡ã大ããã¨ãããè¡ããªãã
ããããå ´åã¯ããã®éä¿¡å¦çãå«ãã ã¡ã½ãããå®ç¾©ãã¦ãããä¸åãå¤å´ã®ãã¹ããæ¸ããã¨ã«ãªãã¨æãã¾ãã
éä¿¡å¦çãé è½ããã©ã¤ãã©ãªãä½ã£ã¦ããã®ã©ã¤ãã©ãªãç¨ããå¦çã«å¯¾ãããã¹ãã«ãªãã¨æãã¾ããããã¹ãæ¹æ³ã¯ã©ã¤ãã©ãªã®è¨è¨(input-outputãä½ã«ãããç)ã«ä¾åããã®ã§ä½ã¨ãè¨ãã¾ããã
ãããããã®ãã©ã¤ãã©ãªèªä½ã®ãã¹ããã¯çµå±ä¸ã®æ§ã«ãªãããããªãããªã¨æã£ã¦ãã¾ãã
ãããæ£ããã®ãã¯åããã¾ããããä»ã®æç¹ã§ã¯ããè¨ãæãããªã¨æã£ã¦ãã¾ãã
ãã®è¾ºã¯èªåãã¾ã Ajaxå¨ãã®ãã¶ã¤ã³ãã¿ã¼ã³ããã¡ãã¨å¦ãã§ããªãã®ã§ãæãããããåå¼·ãããã¨ã§è²ã
æ°ã¥ããå¾ãããããªã¨æã£ã¦ãã¾ããããããJavaScriptèªä½ã«ã¯æ
£ãã¦ããã®ã§ãããã次ã®ã¹ããããã¨ã
Assertion
ããã¯ãå
æ¥è¡ãããShibuya.js㧠id:amachang ããã話ããã¦ããå
容ã§ãã
ããã«ããt-wadaãããAssertionã®æå¹æ§ã«ã¤ãã¦ã¤ã¶ããã¦ããã¨æããã§ããããã®å
·ä½çãªå®è£
æ¹æ³ãå
æ¥ã®amachangããã®ã¨ã³ããªã«æ¸ããã¦ãã¾ããããããé常ã«è©³ç´°ã«ã
ã¦ã¼ã¶ã¼ã¤ã³ã¿ãã§ã¼ã¹ã®ããã«ãã¹ãããããã³ã¼ãã§ã¯ãã¢ãµã¼ã·ã§ã³ã使ãã¨ã³ã¼ãã®è³ªãå¹çããæ¹åã§ãã¾ãã
ãã®æ¹æ³ã¯ãèªåã«ä»ã¾ã§è¶³ãã¦ããªãã£ãé¨åã¨ãã¦ã¨ã¦ã大äºãªæè¡ã ã¨æãã®ã§ã試ãã¦è¡ãããã§ãã
ã³ã³ãã¤ã«ã¨ãªã³ã
ããã¾ã§ã®æµãã§æ¸ããã¹ãã¯ãå®éã«ã¯ä»æ§ã¨ãæ¯ãèãã¨è¨ãããç©ã®ãã¹ããªãã§ãããå®éã«æ¸ãã¦ãããªãã§ãä¾ãã°æ¯ãèãããã¹ãããã³ã¼ãã®æ¯ãèãã¯åã£ã¦ãã®ãï¼
ã¿ãããªã¨ããã¾ã§ä¸å®ã«ãªãå§ããã¨ãè²ã
ç ´ç¶»ãã¦ãã¾ãã®ããã¹ãã®é£ããã¨ããã ã¨å人çã«ã¯æã£ã¦ãã¾ãã
ã¤ã¾ããããããå¤æ°åãééã£ã¦ãããæ§æããã¹ãã¦ãããã
ããããã±ã¢ã¬ã¹ãªãã¹ã£ã¯ããã¡ãããã¹ãã«ãã£ã¦ãã¶ãåºããããããã¾ãããããããã®ãåå ã§ãã¹ããè½ã¡ãã¨ããè¨è¿°ãã¹ããã¹ãå´ã«æã£ããããã®ã¯ç²¾ç¥è¡çä¸ããããã¾ãã(èªåã®å ´å)ã
ã¾ãããªãã¨ããã Java on Eclipse ãªéçºããã¦ããããã¾ãæããªããã®ç¾ç¶ã¯ãJavaScriptã¨ããå²ã¨ãããµããªè¨èªã§ããã©ã¼ãªIDEãJavaç¨æ´ã£ã¦ããªãã¨ãããæãããããã¾ããã(èªåã¯Emacsã®JS2-modeã§ãã£ã¦ã¾ã)
ããããJavaç¨ã§ãªãã¨ãåºç¤çãªé¨åã¯è²ã
ãªãã¼ã«ã«é ¼ãã¾ãã
Webä¸ã®ãã¼ã«ãããã¾ããJSONLint - The JSON Validator.ã¨ããhttp://www.jslint.com/
ãã以å¤ã«æè¿åºã¦æ¥ããã¼ã«ã¨ãã¦ä»¥ä¸ã®ãããªç©ãæãã¾ãã
StyleGuideã¨ãã§ãã«ã¨ã³ã³ãã¤ã©
ã½ã¼ã¹ãã¡ããã¨ä¸å®ã®è¦åã§æ¸ããã¦ãããã¨ã¯JavaScriptã«éããéè¦ã§ãã
ããã§ãã¹ã¿ã¤ã«ã¬ã¤ã(æ§æè¦ç´)çãä½ãã®ã¯ãä¾ãï¼äººã§éçºããã«ãã¦ãæçã ã¨æãã¾ãã
ãã®ã¹ã¿ã¤ã«ã¬ã¤ãã¯èªåã§ä½ã£ã¦ãè¯ããã§ãããèªåã¯ä¿ºã
è¦ç´ã§ã¯ãªãGoogleã®StyleGuideãåãå
¥ãããã¨ã«ãã¦ã¾ãã
çç±ã¯ãGoogleãç·´ãä¸ãããã®ã§ãè¦ç´ãèªãã§ã¦ãç´å¾ã©ãããåå¼·ã«ãªãç¹ãå¤ãã£ãããã¨ããã®ãããã¾ãã
ãããããããªãè¦ãããã¨æã£ã¦ãç¡çãªã®ã§ããã¼ã«ã使ãã¾ãã
è¦ç´ã«åã£ã¦ãããããã§ãã¯ãã¦ãããlinterãæä¾ããã¦ãã¾ãã
How to Use Closure Linter - Closure Tools — Google Developers
使ãæ¹ã¯ç°¡åã§ãå°å
¥ãeasy_installã§ããå
¥ãã¾ãã
ここã«ãè¨äºãæãã¾ããããããã§ææãã¦ãããã°ã¯ããä¿®æ£ããã¦ããããã§ãã
å®éã«æå
ã«æã£ãé©å½ãªJSããã§ãã¯ãã¦ã¿ã¾ãã
gjslint
以ä¸ãbefore
// db.js var db ={ set : function(key, obj){ localStorage.setItem(key, JSON.stringify(obj)); }, get : function(key){ var tmp = localStorage.getItem(key); return (tmp === undefined)? null : JSON.parse(tmp); }, each : function(fun){ try{ for (var i=0; i<localStorage.length; i++){ var k = localStorage.key(i); fun(k,db.get(k)); } }catch(e){ for (var key in localStorage){ if(key === 'key') continue; fun(k,db.get(k)); } } }, del : function(key){ delete localStorage[key]; } };
çµæã¯ä»¥ä¸ã
% gjslint db.js ----- FILE : /path/to/db.js ----- Line 1, E:0002: Missing space after "=" Line 2, E:0005: Illegal tab in whitespace before "set" Line 2, E:0001: Extra space before ":" Line 2, E:0002: Missing space before "{" Line 3, E:0005: Illegal tab in whitespace before "localStorage.setItem" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Line 26, E:0005: Illegal tab in whitespace before "delete" Line 27, E:0005: Illegal tab in whitespace before "}" Found 47 errors, including 0 new errors, in 1 files (0 files OK). Some of the errors reported by GJsLint may be auto-fixable using the script fixjsstyle. Please double check any changes it makes and report any bugs. The script can be run by executing: fixjsstyle db.js
ã§ããã¯ãã§ãã¯ã ãã§ãããæå¾ã«æãããã«ããããä¿®æ£ã¾ã§ãã¦ããããã¼ã«ã¨ãã¦ã
fixjsstyle ãããã¾ãã使ãæ¹ã¯åãã
fixjsstyle
以ä¸ãafter
var db = { set: function(key, obj) { localStorage.setItem(key, JSON.stringify(obj)); }, get: function(key) { var tmp = localStorage.getItem(key); return (tmp === undefined) ? null : JSON.parse(tmp); }, each: function(fun) { try { for (var i = 0; i < localStorage.length; i++) { var k = localStorage.key(i); fun(k, db.get(k)); } }catch (e) { for (var key in localStorage) { if (key === 'key') continue; fun(k, db.get(k)); } } }, del: function(key) { delete localStorage[key]; } };
ãã®å ´å主ã«ã¹ãã¼ã¹ã§æããã¦ãä¿®æ£ããã¦ãã¾ãã
å¥ã®ä¾ã ã¨ãï¼è¡ã80æå以ä¸ã ã£ãããã»ãã³ãã³æãããã§ãã¯åºæ¥ã¾ãã
ãã¨ãGoogleã®è¦ç´ã§ã¯JSDocãæ¸ãã¦ãããClosureã³ã³ãã¤ã©ã§ã³ã³ãã¤ã«æã«ãã§ãã¯ãããã¨ãããããªãã¨ãããã®ã§ã
JSDocã«ã¤ãã¦ããã§ãã¯ãããã¨ãã§ããããã§ãã
(èªåã¯ã¾ã ããã¾ã§ãã£ããã¨ã¯ç¡ããã§ããã)
GoogleClosureCompiler
æå¾ã«ã³ã³ãã¤ã©ã§ãã
ããã§ã¯Google謹製ã®JavaScriptã©ã¤ãã©ãªã®ä¸é¨ã§ãããClosureCompilerãç´¹ä»ãã¾ãã
Closure Compiler Documentation - Closure Tools — Google Developers
ãã®ã³ã³ãã¤ã©ã¯ãããããã³ã¼ãå§ç¸®ãã¼ã«ã®ç¨ã«ã使ãã¾ããã使ããªãã¡ã½ããã®åé¤ãæ§æãã¹ãã§ãã¯ãªã©ããã¦ããã¾ãã
ããã¦ãã³ã³ãã¤ã©ã¯JSDocã§ã¢ããã¼ã·ã§ã³ãè¨è¿°ããã¨ãã³ã³ãã¤ã«æã«åãã§ãã¯ã®æ§ãªãã¨ããã¦ããã¾ãã
ãJSãªã®ã«ã³ã³ãã¤ã«ããã®ããï¼ãã¿ãããªãã¨ãè¨ããã¦ãå°ãã¾ãããGoogle並ã¿ã«è¦æ¨¡ã®å¤§ãããµã¼ãã¹ã®JSéçºã¯ãããããã®ãç¡ãã¨ãã¡è¡ããªããã ããã¨èããã°ç´å¾ã®ä»çµã¿ãã¨æãã¾ãã
ã¾ããã³ã³ãã¤ã«æã®æå®ã«ãã£ã¦ã¯ãã¤ã³ã©ã¤ã³å±éã«ããæé©åããã¦ããã¾ãã
ä¾ãã°
function hello(name) { alert('Hello, ' + name); } hello('New user');
ã
alert("Hello, New user");
ã«ãªãã¨ãããã¨ã§ãã
ããã¾ã§ããã¨ãªãã¨ä¸å®ã«ãªãé¨åãæãã¾ãããã¾ãéãã¯ãªãã§ãããã
è¤æ°ã§éçºãããããªå ´åãªããã¯ç¹ã«ãèªåã§ä¸éå端ãªæé©åããããããã極åä¸å¯§ã«æ¸ãè¾¼ãã§æé©åã¯ã³ã³ãã¤ã©ã«ä»»ããã¨ããé²ãæ¹ãã§ããã¨æãã¾ãã
ã¤ã³ã¹ãã¼ã«ããªãã¦ãここãã試ãã¾ãã
ãªãWebä¸ã§è©¦ããã®ãã¨ããã¨ãã©ãããã®ãã¼ã«ã¯Webãµã¼ãã¹ã¨ãã¦æä¾ããã¦ããããã§ãã
Getting Started with the API - Closure Tools — Google Developers
Closureã¯JavaScriptã®UIã©ã¤ãã©ãªçãæä¾ããã¦ãããã§ãããèªåã¯ã¾ã ãã¡ãã¯ä½¿ã£ããã¨ãããã¾ããã
ã³ã³ãã¤ã©ãã¾ã ç°¡åãªã¨ãããã使ã£ã¦ãªãã§ãããã¡ãã£ã¨ã¥ã¤å°å
¥ãã¦è²ã
試ãã¦è¡ãããã¨æã£ã¦ãã¾ãã
ECMAScript5
JavaScriptã®ä»æ§ã«ãããECMAScript5ã®çå®ãé²ã¿ãå®è£
ãããã°JavaScriptã¯ããªãæ©è½å¼·åãããäºå®ã§ãã
ECMAScript5ã¯ä»ã¾ã§ã®JSã®å¼±ç¹ã®è£å®ãServerSideJSãè¦éã«å
¥ãã¦çå®ããã¦ãããããªã®ã§ãæ®åããã°JSã®éçºã¹ã¿ã¤ã«ã大ããå¤ããããã§ãã
ããã«ä¼´ããJavaScriptéçºã®ãã¤ãã«ããµã¤æ¬ãã®ç¬¬ï¼çãç¾å¨å·çä¸ã§ãä»ã®ã¨ãããããä¸çªè©³ãããã§ãã
Rough Cutsãªãæã«å
¥ãã¾ãã
JavaScript: The Definitive Guide, 6th Edition: Safari Books Online
ä»ããã§ãå°ãã¥ã¤è¦ã¦ããã¨è¯ããã¨æãã¾ãã
mock ã¨ãã¦ã®ãã¡ã¤ã«ã¨localStorage
ããã¯ããã¹ãã¨ãããéçºã®è©±ã§ãã
ãµã¼ãã使ããã«Ajaxãªå¦çã®æåã確èªããã«ã¯ããã¡ã¤ã«ã«ã¬ã¹ãã³ã¹ã¨ãªããã¼ã¿ãè¨è¿°ããæ¹æ³ãããã¾ãã
ä¾ãã°æå¾
ãããJSONãXMLãç´æ¥æ¸ãè¾¼ãã ãã¡ã¤ã«ãç¨æãã¦ãAjaxã®URLããã®ãã¡ã¤ã«ã¸ã®ãã¼ã«ã«ãã¹ã«ããæ¹æ³ã§ãã
ããã¯ãã¹ãã®æã«ã¯ä½¿ããããããã¾ããããéçºã«ããã¦å®éã®ã¢ããªã±ã¼ã·ã§ã³ã®æµããåç¾ããããã«ã¯ä½¿ããªãã®ã§ã
UIã®éçºã¯éçºãµã¼ããç¨æãã¦Ajaxå¦çããã®ãµã¼ãã®APIãå©ãã¦ãããã¨ã«ãªãå ´åã¯å¤ãã¨æãã¾ãã
ãã ãã¯ã©ã¤ã¢ã³ãã®éçºãããã®ã«éçºãµã¼ãããã¡ãã¡èµ·åããã®ã¯é¢åã§ãããéçºã®ããã«ä»®APIãsinatraããflaskããã§çµããã¨ãã§ãã¾ããããããæéã§ãã
CouchDBã¨ããæãæããã§ãããå®éããã¨APIãCouchDBã«å¤å°ãªãã¨ãããã¨ããåé¡ãæãã¾ãã
ããã§ããã®éçºãµã¼ãã®ã¢ãã¯ã¨ãã¦ãlocalStorageã使ããªããã¨æã£ã¦è©¦ãã¦ãã¾ãã
ã¤ã¾ããã¯ã©ã¤ã¢ã³ãéçºæã«
$.post(url,data,callback);
ã§ã¯ãªã
lib.post(url,data,callback);
ã¨ããããã«ï¼æä¸ã«è¢«ãã¾ãã
ãã®libã©ã¤ãã©ãªã®postã¡ã½ããã¯ãéçºæã¯localStorageã«å¯¾ãã¦ãæ¬çªã¯æ¬çªãµã¼ãã«å¯¾ãã¦ã¢ã¯ã»ã¹ããæ§ã«å®è£ ãã¾ãã
var lib = { post : //æ¬çªãµã¼ã㸠} var mock = { post : //localStorage㸠} lib = mock//æ¬çªã¯ãããæ¶ã $(function(){ lib.post //æ¸ãæãã¦ãã®ã§å®ã¯mock });
ä¸æ¸ãã®æ¹æ³ã¯ãã£ã¨ãã£ãè¯ãããã°è¯ãã§ãããã¨ã«ããåãåããæ§ã«ãã¦ããã¾ãã
mockã¯æ¬çªã¯ä¾å解決æã«å¤ãããminifyããã¨ãã«ãã£ããæ¶ãã®ã§ãè¯ãã¨æãã¾ãã
ãªã®ã§ãå
ã»ã©ãæ¸ããæ§ã«ãå®éã®APIã«å¯¾ããã©ã¤ãã©ãªãæ¸ãã¦ããããå¾¹åºçã«ãã¹ããã¾ãã
ããã¦ãmockããã®ãã¹ããå
¨ã¦éãæ§ã«å®è£
ãã¾ãã
ããã¨UIå´ã®éçºæã¯APIãã決ã¾ã£ã¦ããã°ããµã¼ãç¡ãã§å®è£
ãé²ãããã¾ãã
å®è£
ä¸ã®htmlã¯http://ãããªãã¦file://ã§éãã¦éçºåºæ¥ã¾ãã
ãã¡ãããããã¯ã¾ã ã¢ã¤ãã¢ã®æ®µéãªã®ã§æ¬ ç¹ã¯å±±ã»ã©ããã¾ãã
- localStorageãç¡ãã¨ãã¡(uupaa.js使ãã°ããã)
- FireFoxã¯file://ã§éãããã¡ã¤ã«ã®localStorageã®å 容ãæ´æ°æã«ã¯ãªã¢ãããbugzilla,ここも
- ç¾ç¶ã®localStorageã¯æååããå ¥ããªã(JSON.parse,JSON.stringifyã使ã£ãã©ããã¼ãæ¸ã)
- åºãå ¥ããåæå¦ç(workerã¨setTimeoutã§ããã¯ã°ã©ã¦ã³ãéä¿¡ãã¦ãã£ã½ãããããindexed dbãå®è£ ãããã°éåæAPIããããã)
- ã¹ãã¼ã¿ã¹ã³ã¼ãã¯ï¼(ã¨ã©ã¼ã¯çä¼¼çºçãããããç¡ã)
- ã¡ã³ããã³ã¹ã³ã¹ããé«ã(ããã¯ã¯ã©ã¤ã¢ã³ãéçºã®æéã¨ã®ãã¬ã¼ããªãã)
APIã«ãã£ã¦ã¯ã©ã¤ã¢ã³ãã¨ãµã¼ããççµåã«ããã¨ãããã¨ã¯ãå®éã®éçºã¯APIãåã決ãã¨ãã¦å®ç¾©ãããã¨ãã¯ã©ã¤ã¢ã³ããµã¤ãããµã¼ããµã¤ãã®éçºã並è¡ãã¦é²ãå¯è½æ§ãããã¾ãã
ãã®å ´åã¯ãä¸éå端ãªãã¡ã¤ã«ã¢ãã¯ãã¡ã³ããç¶ããããã¡ãªãããããå ´åãå¤ããã¨æãã¾ãã
localStorageèªä½ã®å®è£
å·®ã¯çµæ§æãã®ã§ãäºåã«localStorageã®æåèªä½ããã¡ãã¨ææ¡ããå¿
è¦ãæãã¾ãããããã¯ä¸åããã°è¯ãããããã¯ä¿¡é ¼ã§ããã©ã¤ãã©ãªãå
¥ãããã¨ã§è§£æ±ºã§ãã¾ãã
ããããã¨ãåã
ã®ã¡ã½ããã®ãã¹ãã«ã使ããããGET-POST-PUT-DELETEãé§ä½¿ããã¯ã©ã¤ã¢ã³ãã®éçºãããã¡ãã¡ãµã¼ãã«ç¹ããã«åãã確èªåºæ¥ã¾ãã
ã¾ããããç¨åº¦é²ãã ãéçºãµã¼ãã«ç¹ãã æ¹ãããã§ããããããã¦è¦ã¤ããã®ã¯å²ã¨mockèªèº«ã®ãã°ã ã£ãããã¾ãã
ãããè¾æ±å¼·ãç¹°ãè¿ãã°ãmockã®å®æ度ã¯é«ããªãã®ã§ãæ©è½æ¡å¼µãéã«API追å ã®ãããã¿ã¤ãã¨ãã¦ä½¿ããããããã¾ããã
欲ãåºãã°ããã®mockã§è¢«ããAPIãRESTfulã«ãããã¨ãæèãã¦ããã°ããã®å¾å¥ã®ã¢ããªãéçºããæã«APIãRESTfulã«ãããã¨ã§ãmockã¯ããç¨åº¦ä½¿ãåããå¹ãããããã¾ããã
çµ±ä¸çã¤ã³ã¿ãã§ã¼ã¹ã«è¢«ããã ãã®ã¤ã³ã¿ãã§ã¼ã¹ã§ãã以ä¸ãããã§ãªãã¨ããããã¯ãã
ãã¨ãç¾æç¹ã§ç¾å®çãªã¯ã©ã¤ã¢ã³ããµã¤ãã¹ãã¬ã¼ã¸ã¯localStorageã§ãããè²ã
ãããã¨ããã¨indexed Databaseã®å®è£
ã欲ããã¨ããã§ã(éåæã¨ã)ã
ã¾ããããã¾ã§ããã¨å¦æ³ã§ããã
ä¸å¿ãã£ã¦ã¯è¦ã¾ããã
localStorageをmockにしてjsonengineのクライアントを開発する - Block Rockin’ Codes
localStorageの挙動と簡単なラッパー - Block Rockin’ Codes
ããã¯ããå°ãç¶ãã¦ã¿ããã¨æã£ã¦ãã¾ãã
(ãªãã©ãè¨ãã¾ããå®ç§ã§ã¯ãªãã§ããããã¾ã§ã¢ã¤ãã¢ã)
ã¾ã¨ã
ãªãã¨ããããJSã®éçºã«ã¤ãã¦ã¯ä»ã®è¨èªããé£ããã¨æããé¨åãèªåã¨ãã¦å¤ããè²ã
試ãã¦ããã§ããä¸ã
次ã®ã¹ãããã«é²ãç¡ãæããããã®ã§ãã¨ããããè²ã
ã¢ã¦ãããããããã¨ã§æ´çãããã¨æãæ¸ãå§ãã¾ããã
ãããèµ·ç¹ã«ããä¸åº¦è²ã
è¦ç´ãããã¨æãã¾ãã