JavaScriptãã¬ãŒã ã¯ãŒã¯Backbone.jsã®äœ¿çšã«ã€ããŠå€ãã®ããšãæžãããŠããŸãããããã»ã©åçŽã§ç°¡æœã§ã¯ãããŸããã ãã®æ¬ ç¹ãä¿®æ£ããWebã¢ããªã±ãŒã·ã§ã³éçºè
ã«ããã®ãã¬ãŒã ã¯ãŒã¯ããªã圹ã«ç«ã€ã®ããäžè¬çã«ã¯ã©ã®ããã«æ©èœããã®ãããã§ããã ãç°¡åã§ãã¢ã¯ã»ã¹ãããããç°¡æœã«äŒããããã«ããŸãã Backbone.jsã®å°é家ãšå°é家ïŒæéãç¡é§ã«ããããšã¯ã§ããŸããããã®ç©èªã¯åå¿è
åãã§ãã æ£çŽã«èšããšããã®èšäºãèªãããã«Railséçºè
ã§ããå¿
èŠã¯ãããŸãããMVCãã¬ãŒã ã¯ãŒã¯ã䜿çšãããã¹ãŠã®äººã«ãã®èšäºã圹ç«ã€ããšãé¡ã£ãŠããŸãã
ãããã£ãŠãMVCã¢ãããŒãã䜿çšããå¹³åçãªãããžã§ã¯ããæ³åããŠãã ããã ããã¯ãããŸããŸãªã¿ã€ãã®é¢ä¿ã«ãã£ãŠäºãã«é¢é£ä»ããããããã€ãã®ã¢ãã«ïŒéåžžã¯çŽ10ãã15ïŒãæã€ãªã³ã©ã€ã³ã¹ãã¢ã§ããå¯èœæ§ããããŸãã ãããžã§ã¯ãã«ã¯ãé©åãªæ°ã®ã³ã³ãããŒã©ãŒãããŸããŸãªåºåããã€ã¹çšã®2ã3ã®ã¬ã€ã¢ãŠããåå空é/ api / v1ã®è€æ°ã®ã³ã³ãããŒã©ãŒãå€ãã®ãã¥ãŒããã³éšåçãªã³ã³ãããŒã©ãŒãå«ãŸããŸãã ããã¯ãã¹ãŠæšæºãšããŠæ©èœãããã©ãŠã¶ãŒã¯ãªã¯ãšã¹ããéä¿¡ããã³ã³ãããŒã©ãŒã¯ããŒã¿ãµã³ãã«ãäœæããããããview'shkiã«éä¿¡ãããããã¯ãã©ãŠã¶ãŒã§ãŠãŒã¶ãŒã«éä¿¡ãããŸãã ç¹å®ã®ã¢ãã«ã®ç¹å®ã®ã€ã³ã¹ã¿ã³ã¹ãæ€çŽ¢ãããæ€çŽ¢ãæ©èœããããããŒãžäžã«ãããã®èŠã€ãã£ãã€ã³ã¹ã¿ã³ã¹ã®è¡šç€ºãäœæããã«ãŒããå«ãçµæã®ãã¥ãŒããããAPIãä»ããŠãµãŒããŒãšéä¿¡ããAndroidçšã®ã¢ãã€ã«ã¢ããªã±ãŒã·ã§ã³ããããŸãïŒã³ã³ãããŒã©ãŒãèŠæ±ããã¡ãªã¿ã«ãnamespace'e / api / v1ã§åŸ
æ©ããŸããå€ãã®å Žåãåæ§ã®ãéåžžã®ãã³ã³ãããŒã©ãè€è£œããç°ãªã圢åŒã§ã®ã¿æ
å ±ãæäŸããŸãã
次ã«ãã¯ã³ã®ã¢ãŒãããªã³ã«ããŸãã ãããžã§ã¯ãã«ã¯å¿
ãJavaScriptã³ãŒããå«ãŸããŠããããšãç¥ã£ãŠããŸãã ããŸããŸãªWebã¢ããªã±ãŒã·ã§ã³ãJavaScriptã䜿çšããŠããŸããŸãªåé¡ã解決ããŸããã»ãŒãã¹ãŠã®å Žæã§jQueryããã©ã°ã€ã³ãjQuery UIããã®ä»ã®ã¢ãžã¥ãŒã«ããã³ãã©ã°ã€ã³ã®æç¡ã«ããããã䜿çšãããŸãã ããã§ã¯ãè£
食ã«é¢é£ããããããçš®é¡ã®ãšãã§ã¯ããããããããŠã³ã¡ãã¥ãŒããã¬ãŒãïŒããããèŠçŽ ãªã©ãäœæããããã®JavaScriptã®äœ¿çšãæ€èšããŸããã ããŒã¿ã®æäœãæ€èšããŸã ã
ãããã£ãŠãWangiã¢ãŒãã§ã¯ã補åã¢ãã«ïŒãªã³ã©ã€ã³ã¹ãã¢ãããããšãèŠããŠããŸããïŒïŒããããšæ³å®ã§ããŸãããŸãã確ãã«ãããã€ãã®åºæºã«åŸã£ãŠè£œåã®ãªã¹ãã衚瀺ã§ãããããŒãžã£ãŒã€ã³ã¿ãŒãã§ã€ã¹ããããŸãã ãããŒãžã£ãŒã¯ãæ°ãã泚æã®æ°ã瀺ãå®ååãããã€ã³ãžã±ãŒã¿ãŒã䜿çšããŠãããã«æ°ãã泚æã衚瀺ãããããšã確èªããŸãã ãã®ã€ã³ãžã±ãŒã¿ãŒã¯æ¬¡ã®ããã«æ©èœãããšæããŸãïŒdocument.readyïŒïŒã§ã¿ã€ããŒãäœæãã60ç§ããšã«/ api / v1 / kolichestvo_zakazovã®ãããªã¢ãã¬ã¹ãèŠæ±ããŸãã ãŸãããŸãã¯/ api / v1 / orders / new / count ãèšèªåŠã«å¯Ÿããããªãã®æ
床ã«äŸåããŸãã ã¿ã€ããŒãéå§ããé¢æ°ã«ã¯ã以åã«æå®ããURLã§ããŒã¿ãåä¿¡ããjQuery ajaxã§èšè¿°ããããªã¯ãšã¹ãããããŸãã ãªãã§ïŒ ããããªããçã¯å©ããªã ããããã°ã©ããŒã¯{ordersïŒ{newïŒ3}}ã®ãããªæ£ããjsonãéä¿¡ããå¥ã®ããã°ã©ããŒã¯æ°å3ã ããéä¿¡ã§ããŸãã3çªç®ã®ããã°ã©ããŒã¯ãµãŒããŒåŽã§ã¬ã³ããªã³ã°ããå¿çã§ããã¥ã¡ã³ãå
ã®é©åãªå Žæã«ããã«æ¿å
¥ã§ããHTMLã³ãŒãå
šäœãéä¿¡ã§ããŸãã äžè¬çã«ããŽã¡ã³ã¬ã¯ããã§å©ãã«ã¯ãªããŸããã秩åºãæ³åŸãé©ãã®æ倧ã¬ãã«ã¯ãããŸããã ããããæãéèŠãªã®ã¯ãããŒã¿ãåãåã£ãåŸãåçŽã«èšãã°ãããŒã¿ã倱ãããããšã§ãã ã¯ã©ã€ã¢ã³ãåŽã¯äœããåãåãããã®ããŒã¿ã䜿ã£ãŠäœããããã®ã§ã圌ãã¯ãã¯ãããªãã«å©çããããããªãã§ãããã
å®ç§äž»çŸ©è
ã®å²åŠè
ã®èŠ³ç¹ãããããã¯ã²ã©ãã§ãã ãµãŒããŒåŽã§ããŒã¿ãæŽçããæ°çŸã®æ¹æ³ããã§ã«èæ¡ãããŠããå ŽåïŒMVCã¢ãããŒããDBMSã¯åºæ¬çã«æ
å ±ãæŽçããããã®æ¹æ³è«ã§ãïŒã次ã®ã¹ãããã¯ãã¯ã©ã€ã¢ã³ãåŽã§ããŒã¿ãæŽçããããšã§ã ã
Backbone.jsã¯äœãæäŸã§ããŸããïŒ ç§ã®æèŠã§ã¯ãæåã®æãéèŠãªããšã¯ãããŒã¿ã®é åºä»ãã§ããããã¯ã䜿ãæ
£ããMVCãä»ããŠBackbone.jsã«å®è£
ãããŸãã ãããã©ããªããã«ã€ããŠç°¡åã«ïŒ
1.ã¢ããªã±ãŒã·ã§ã³ã«ã¢ãã«ããããŸãã ãŠãŒã¶ãŒã補åã泚æ...ãã°ããããBackbone.jsã䜿çšãããšããããã®ã¢ãã«ãã¯ã©ã€ã¢ã³ãåŽã§èª¬æã§ããŸãã ãããŠããããã®ã¢ãã«ã¯æ¬ç©ã§ããæ°ããã€ã³ã¹ã¿ã³ã¹ãäœæããæ¢åã®ã€ã³ã¹ã¿ã³ã¹ã®ãã£ãŒã«ããç·šéããäžèŠãªã€ã³ã¹ã¿ã³ã¹ãåé€ã§ããŸãã åŸã§ã¹ããŒãããæ
å ±ãååŸãããããµãŒããŒãã泚æã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãååŸãããããããã®é¢æ°ãäœæããããšã¯ã§ããŸããã 泚æã«ã¯å€ãã®äžèŠãªæ
å ±ãå«ãŸããŠããå¯èœæ§ããããŸãã ãã¶ãã ãã ããã¯ã©ã€ã¢ã³ãåŽã§å¿
èŠãªé åºãã£ãŒã«ãã®ã¿ãJSONã§éä¿¡ããããšãçŠæ¢ãã人ã¯ããŸããã ãããŠãããªãã®ä»®æ³ãããŒãžã£ãŒã¯ã泚ææ°ã«å ããŠãããããã®åèšã«å ããŠãçªç¶ã衚瀺ããããããããŸãã-ãããŠãç§ãã¡ã¯ãã§ã«ãã¹ãŠãè¡ã£ãŠããŸããæ
å ±ã¯ãã§ã«ããã«ãããŸããããã衚瀺ããã ãã§ããªããžã§ã¯ãã¯ãã§ã«ã¯ã©ã€ã¢ã³ãäžã«ããããµãŒããŒãšåæããŠããŸãã ãããŠããããŒãžã£ãŒããã®å Žã§ãã®éé¡ãå€æŽãããå Žåã¯ã©ããªããŸããïŒ åé¡ãããŸããã æ°ããèªå®¶è£œã®é¢æ°ãèšè¿°ããããããã³ãŒãã«order.saveïŒ{ïŒcost => this.input.valïŒ}ïŒãèšè¿°ããæ¹ãã¯ããã«åªããŠããŸããã¢ãã«ã¯ãGETãPOSTãPUTãããã³DELETEãªã¯ãšã¹ããéä¿¡ããURLãç¥ã£ãŠãããããšãã°ãå¿
èŠãªå Žåãproduct.deleteïŒïŒã¯DELETEã/ api / v1 / products / 75ã«éä¿¡ããŸãïŒ75ã¯ãã®ã¢ãã«ã®IDã§ãïŒã
2.å€ãã®åæ§ã®ãããã¯ãå¿
èŠãªã¢ããªã±ãŒã·ã§ã³ã®ããŒãžããããŸãã 補åæ€çŽ¢çµæã¯äžäŸã§ãã å補åã¯ãdiv class = 'product'å
ã«é
眮ã§ããŸãã ãµãŒããŒäžã§ãã®ããŒãžãäœæããåŸããã®ãšãªã¢ãšèšèŒãããŠãã補åãšã®æ¥ç¶ã¯å€±ãããŸãã ãã®ããŒãžã§äœãããããå Žåã¯ããã®ãšãªã¢ã«é¢é£ãã補åãå床ç¥ãããæ¹æ³ãèæ¡ããå¿
èŠããããŸããããšãã°ã補åIDã§ããŒã¿å±æ§ãæå®ãããã©ãŒã ããªã¯ãšã¹ãã§äœ¿çšããŸããBackbone.jsã¯ãã®ãããªãã€ã³ãã£ã³ã°èªäœãå®è£
ããŸãã ããã¯ããã¥ãŒããšåŒã°ããããã«å€ãã®ããšãè¡ãããŸããããšãã°ã補åãšãªã¢ã¯è£œåã®å€æŽã«å¿çããŠãããèªäœãåæç»ã§ããŸãã ãã®é åå
ã§ã¯ãProductã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ïŒãŸãã«ãã®ã¯ã©ã¹ïŒã«ã¢ã¯ã»ã¹ã§ããã¯ã©ã€ã¢ã³ãåŽã§äœã§ãå®è¡ã§ããŸãã Backbone.jsããµãŒããŒãžã®å€æŽãä¿åããããã«ãåŸã§ä¿åããããšãå¿ããªãã§ãã ããã
ãšããã§ã 補åããŒãã«ã®ããã€ãã®ãã£ãŒã«ãã§ãã£ã«ã¿ãªã³ã°ãããã§ããïŒ ããã¯è¿œå ã®ã¢ã¯ã·ã§ã³ãå¿
èŠãšããªãããšããã§ã«æšæž¬ããŸãããïŒ ãµãŒããŒã«äœããéä¿¡ããå¿
èŠã¯ãããŸãããåãããŒãã«ãååŸããå床ãã£ã«ã¿ãªã³ã°ããã ãã§ãã ãã§ã«ãªããžã§ã¯ãããããŸãã1è¡ã®JavaScriptã³ãŒãã§è£œåã®ã³ã¬ã¯ã·ã§ã³ããã£ã«ã¿ãªã³ã°ããã ãã§ãã ãããŠãã¯ãã圌ããèªåèªèº«ãåæç»ããŸãã補åã®å€æŽæã«ãã®è£œåã«é¢ããæ
å ±ãããããŒãžã®äžéšãæŽæ°ããããšãBackboneã«äŒããå¿
èŠãããã®ã¯äžåºŠã ãã§ãã
3.ãŸããããŒã¿ã®æŽçã®ãããã¯ã§ãã ã¯ã©ã€ã¢ã³ãåŽã®MVCãã¬ãŒã ã¯ãŒã¯ã䜿çšãããšãå Žåã«ãã£ãŠã¯ãã¯ã©ã€ã¢ã³ããšãµãŒããŒéã®ãã©ãã£ãã¯ãæžãããŠjsonã®å°ããªæçã亀æããããšãã§ããããã«ãã倧éã®ãã©ãã£ãã¯ãç¯çŽã§ããŸãã 顧客ã«éä¿¡ãã顧客ããã®ããŒã¿ã®ã¿ãåä¿¡ããŸãã顧客ã«ããŒãžå
šäœãåžžã«éä¿¡ããããã§ã¯ãããŸããã ãã¹ãŠã®javascriptãšcssãäžåºŠããŒãããŠãã£ãã·ã¥ããããšãã§ããŸãããããããšã奜ããªããã«æ³šæãããããŒã¿ã®äº€æã®ã¿ãè¡ãããŸãã
çµè«ã®ä»£ããã«ïŒãã¡ããããã®ã¡ã¢ã¯Backbone.jsã®å¯èœæ§ã®5ïŒ
ã«ã¯è§ŠããŠããŸããããç§ã¯ããã«ã€ããŠè©±ãããã¯ãããŸãããããµãŒããŒãšãã©ãŠã¶ãŒã®äž¡æ¹ã§æ
å ±ãé çªã«ç¶æããã¢ã€ãã¢ã«ã€ããŠã¯è¿°ã¹ãŸããã ãœãããŠã§ã¢éçºè
ã¯ç§ãã¡ã®åšãã®äžçãæŽçããäžçãã¢ãã«ã«åå²ãããã®ããããã£ãåäœãç¶æ
ãããã³çžäºäœçšãç¹å®ããŸãã æ¥éã«å€åããã€ã³ã¿ãŒãããã¯äºå®äžæšæºã«æºæ ããŠããŸããïŒãŸããé åºã®æè¯ã®äŸããã¯çšé ã4ãŸãã¯5ã®CSSä»æ§ãé€ããã¯ã©ã€ã¢ã³ãã³ã³ãã³ãã«é¢ããæšæºã¯ãããŸããïŒã ãã®ãããªäŸ¿å©ãªæ¹æ³ã§ãããã®ã«ãªã¹ã®ãšã³ããããŒãå¢ãããŸããã-ããŒã¿ãæ§é çãªé åºãšåæç¶æ
ã«ä¿ã¡ãŸãïŒ
å®ç§äž»çŸ©è
ã®éçºè
ã§ãããæ¬æãæã£ãŠãæž
èŽããããšãããããŸããã
PSèšäºãã³ãã¥ããã£ã«ãšã£ãŠèå³æ·±ããã®ã§ããããšãå€æããå Žåã¯ãèšäºã®åé ã§èª¬æããã¹ãã¢ã®äŸã䜿çšããŠBackbone.jsã䜿çšãããšãäœãããè¯ããªããã«ã€ããŠã®äŸãšãšãã«ãç¶ç·šãæžããŸãã