ç¶ç·¨ã®ç´¹ä»
ç¶ç·¨ ãã¯ã俺ã®MVCã¯ééãã¦ãã in Backbone.js ãæ¸ããããã£ã¡ã®ã»ããæçãªæ å ±ãä¹ã£ã¦ãã¨æããã©é¢ç½ããªããããããªã
以ä¸æ¬ç·¨
MVC ã®è©±ã¨å®æã®è©±ã¨æ¿æ²»ã®è©±ã¨éçã®è©±ã¯ãã¦ã¯ãããªãããã§ãããããªã®ç¥ãã俺ã¯ããã
ã¯ã©ã¤ã¢ã³ããµã¤ã MVC ã®è©±
ãããã MVC ã®åºèªã GUI ã¢ããªã±ã¼ã·ã§ã³ã®ããã«çã¾ãã¦ãããã®ãªã®ã§ãã¯ã©ã¤ã¢ã³ããµã¤ã MVCããªã©ã¨è¨ãè¨ãæ¹ãããªããã°ãªããªãç¶æ³ããã§ã«æãã®ã ããã©ãã¾ãããã¯ããã¦ããã
ããã¡ã¯ Backbone.js ã使ã£ã¦ãããã MVC ã§ã¯ã©ã¤ã¢ã³ããµã¤ããä½ããã¦ãã¦ä¿å®æ§ãé«ãã§ããã¿ãããªãã¨ãè¨ã人éããã¾ã«ããããBackbone.js ãã¤ãã£ãããï¼ããã㯠Marionette.js ã使ã£ããããï¼ã¨ãã£ã¦èªåçã«ãåã®ã¢ããªã±ã¼ã·ã§ã³ã MVC ã«ãªãããã§ã¯ãªãããã¬ã¼ã ã¯ã¼ã¯ã®ä½¿ãæ¹ãå¦ã¶åã«ãã®ãã¬ã¼ã ã¯ã¼ã¯ã®èæ¯ãå¦ã¹ã
ãã®ã¨ã³ããªã¼ã§è¨ããããã¨ã¯ã²ã¨ã¤ãããªãã
V ã M ã®æä½ã¡ã½ãããå¼ã¶ã®ã¯MVCã§ã¯ãªãï¼ï¼ï¼ï¼ï¼
Backbone.js 㯠M 㨠V ã®ããã®åºåºã¯ã©ã¹ï¼ã¨ããè¨ãæ¹ã¯ JavaScript ã«ããã¦ã¯æ£ç¢ºã§ã¯ãªããã¾ãããã£ã¦ï¼ã¯æä¾ãã¦ããããC ã®ããã®åºåºã¯ã©ã¹ã¯æä¾ãã¦ããªãããã®ããã ããããBackbone.js ã®ãå ¥éãã¨ã³ããªã¨ããããããããã®ãã¤ã§ã¯ãView ã« Model ãæããã¦ï¼ããã¾ã§ã¯ããï¼View ã®ã¤ãã³ããèµ·ãã£ãã¨ãã« View ããç´æ¥ãã® Model ã®ç¶æ ãå¤ããã¡ã½ãããå¼ãã§ããã¿ãããªãã¨ããã¦ãããã¨ãå¤ãã
ãã¡ãããBackbone.jsã®ã使ãæ¹ããç¥ãã ããªãããã§ååã§ã¯ããã®ã ããã©ããBackbone.jsã使ãããã¨ãªãã»ã©ãããªãã®è¦æ¨¡ã®å®éã®ã¢ããªã±ã¼ã·ã§ã³ã§ãããªãã¨ããã¦ãããç ´ç¶»ãç®ã«è¦ãã¦ããã
ãªã®ã§ã¡ãã㨠MVC ããããããã¾ãããã
Model
- ã¢ããªã±ã¼ã·ã§ã³ã®è«ççãªé¨åã®æä½ãæ
å½ããã
- ãã®ãè«ççãã¨ããã®ã¯ãããã«ããããã©ãè¨ãæããã°ãUIã«é¢ä¿ãªãé¨åããæ å½ããã¨æãã°ããã
- Model ã®ç¶æ ã«å¤åãèµ·ãã£ãã¨ãã«ã¯ã¤ãã³ããçºç«ããã
- ãã Model ã¯è¤æ°ã®Viewã«ç£è¦ããã¦ããå¯è½æ§ããã
- ã¢ããªã±ã¼ã·ã§ã³ã®è«ççãªé¨åã®æä½ãæ
å½ããã
View
- Modelã®ã¤ã³ã¹ã¿ã³ã¹ãæã¡ãã®ã¤ãã³ããè³¼èªããã
- Modelã®ç¶æ ãããã£ãã¨ããã¤ãã³ããåãåã£ãããModelã®ã¤ã³ã¹ã¿ã³ã¹ã®ããã¼ã¿åå¾ç¨ãã®ã¡ã½ãããå¼ã³UIãæç»ããã
- Modelã®ã¤ã³ã¹ã¿ã³ã¹ãä¿æãããããModelã®ã¤ãã³ããè³¼èªããããããModelã®å¤ãåå¾ï¼æä½ã§ã¯ãªãï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ãããããã«ä¿æããã®ã§ãããViewãModelã®å¤ãå¤æ´ããã¡ã½ãããå¼ã¶ãã¨ã¯ãªãï¼ï¼ï¼ï¼ï¼
- æ®éã¯ã¦ã¼ã¶ã¼ã®ã¢ã¯ã·ã§ã³ï¼ãªãããã¯ãªãã¯ããããã¨ãããããã®ï¼ã¯ãã® View ãåãåãããã®ã¨ãã¯ã¤ãã³ããçºç«ãã
- ã»ãã® View ã¨åã Model ã®ã¤ã³ã¹ã¿ã³ã¹ãç£è¦ãã¦ããå¯è½æ§ãããããã®å ´åããã® Model ã®ç¶æ ãå¤ããã¤ãã³ããçºè¡ãããã¨ãã¯ãã® View ã¨èªåãåæç»ããããã¨ã«ãªãã
Controller
- Model ã®ã¤ã³ã¹ã¿ã³ã¹ãä¿æããã View ã®ã¤ã³ã¹ã¿ã³ã¹ãä¿æããã
- View ã®ã¤ãã³ããè³¼èªãã¦ããã®ã¤ãã³ãã«å¿ã㦠Model ã®æä½ã¡ã½ãããå¼ãã ã View ã®æä½ã¡ã½ãããå¼ãã ãããï¼ãã®ã¨ãå¿ è¦ãªãã°Viewã®ãã¼ã¿ãåç §ããï¼ã
- è¤æ°ã®ç¨®é¡ã® Model ã®ã¤ã³ã¹ã¿ã³ã¹ãView ã®ã¤ã³ã¹ã¿ã³ã¹ ãæã¡å¾ãï¼ã¦ãããå¤åã»ã¨ãã©ã®å ´åæã¤ï¼
ããã§ãããã©ãã«ããView ã Model ã®æä½ã¡ã½ãããå¼ã¶ãã¨ãããã¿ã¼ã³ãåºã¦ããªãã§ãããéè¦ãªã®ã¯ä»¥ä¸ã®ç¹ã
- Viewã¯Modelã®æä½ã¡ã½ããã¯å¼ã°ãªããå¼ãã§ããã®ã¯Modelã®ãã¼ã¿åå¾ç¨ã¡ã½ããã ãã§ããã
- Model ã®æä½ã¯ View èªèº«ã§å¦çãããããªãã C ã«ä¸åº¦æ¸¡ãã
ã§ã¯ãªãããããã¹ããªã®ãã
V 㨠M 㯠1 対 1 ã§ã¯ãªã
ã²ã¨ã¤ã®çç±ãããã§ãããã¨ãã°ãArtist ã¢ãã«ã¨ Song ã¢ãã«ãããå ´åãèãã¾ãããã®ã¨ããä¾ãã° ArtistsView ã®ãã¡ç¹å®ã® Artist ãã¯ãªãã¯ããã¨ãã«ã¯ã
- ãã®Artistãé¸æç¶æ ã«ãã
- æ²ä¸æ¬ã«ãã®Artistã®æ²ä¸è¦§ã表示ãã
ã¨ãããããªæä½ãå¿ è¦ã¿ãããªãã¨ã¯ããããã¨æããï¼ãããããããè³¼å ¥æ¸ã¿ãã©ãã調ã¹ã¦ããã¨ããå ¥ããããããªãï¼
ãã®ã¨ããV ãç´æ¥ M ãå¼ã¶ãããªæ§é ã«ãªã£ã¦ãã¨ãArtsitView ã SongModel ãæä½ããå¿ è¦ãããããã£ããã£ã§ãArtistViewã¯ArtiestModelããæã£ã¦ãªãã¦SongModelã¯ã©ãããå¼ã¹ã°ãããã ããããããã¤ã³ã¹ã¿ã³ã¹ãããã¡ããï¼ã¨ãã°ãã¼ãã«ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ã«ããã¡ããã¿ããã«ãªã£ã¦æ§é ããã¡ããã¡ããã¡ããã¨ãªã£ã¦ãã¾ãããã§ãã
ããã C ã V ã®ã¤ãã³ããè³¼èªãã¦ããã¦ãããªããã¤ãã³ã㯠C ã§ãã³ããªã³ã°ããããã«ãã¦ãããã°ãC ã¯å¿ è¦ãª M ã®ã¤ã³ã¹ã¿ã³ã¹ã®æä½ã¡ã½ãããå¼ãã§ãããã°ããããã®ã¨ããã¯ãªãã¯ããã V ãã©ã㪠M ãä¿æãã¦ããããªãã¦ãã¨ã¯èããªãã¦ããããªããªãã° C ã¯ããã㪠M ãæã£ã¦ããããããã責åã ããã ããã㦠C ã M ã®ç¶æ ãå¤ããã¡ã½ãããå¼ãã ãã¨ã§ M ã®ç¶æ ãå¤åããã°ããã® M ãç£è¦ãã¦ãã V ã¯èªåçã«ç»é¢ãæ´æ°ãããã¨ããã¹ã³ãã¼ã§ããã
ãããMVCã®åºæ¬ãããå®ã£ã¦ä½ã£ã¦ã¦ãããããç ©éãªã¢ã¬ããã£ã¦ãããã解決ããããã« MVVM ãªã©ã® MV* ãªãã¿ã¼ã³ãåå¨ããããã ããã©ãããã¯ãããã¨ããããã®åºæ¬ãããã£ã¦ãã°ã»ãã®ãã¤ãããªããããå¿ è¦ãªã®ãããç解ã§ããã¯ãã
大äºãªã®ã¯ãã¤ãã³ãç£è¦ã®æ¹åãçµ±ä¸ãããã¨ãæä½ã®æ¹åãçµ±ä¸ãããã¨ããã¼ã¿åå¾ã®æ¹åãçµ±ä¸ãããã¨ã§ããããã®ããã
- ç£è¦ã®æ¹åã«ã¤ãã¦ã¯
- C 㯠V ã®ã¤ãã³ããç£è¦ãã
- V 㯠M ã®ã¤ãã³ããç£è¦ãã
- æä½ã®æ¹åã«ã¤ãã¦ã¯
- C ã M ãæä½ãã (ãã®ã¨ãMã®ã¤ãã³ããçºç«ãããVãåæç»ããããï¼
- C ã V ãæä½ãã ï¼Mãä»ããªããããªæä½ããã¨ãã°è¡¨ç¤ºãã¦ããªãããæ¶ãã¨ãï¼
- ãã¼ã¿ã®åå¾ã®æ¹åã«ã¤ãã¦ã¯
- V ã M ã®ãã¼ã¿ãåå¾ããï¼æä½ã¯ããªãï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
- C ã V ã®ãã¼ã¿ãåå¾ãã
ã¨ããã®ããå®ãã¨ããã§ãããã®ã»ãã®æ¹åã®æä½ãç£è¦ãè¡ããã¦ãããããã¯ã¤ããã«ããã ã¨è¨ã£ã¦ããã¨æããã¾ãåç´ãªæä½ãªãã°Vããç´æ¥Mãå¤åããã¦ããã¨ãã人ãããããåç´ãªãã®ã§ãã M ã®æä½ã¯ C ã«éç´ãããã»ãã責åãã¯ã£ãããã¦ããã¨æãã
大äºãªãã¨ãªã®ã§ããä¸åº¦
- ç£è¦ã®æ¹åã«ã¤ãã¦ã¯
- C 㯠V ã®ã¤ãã³ããç£è¦ãã
- V 㯠M ã®ã¤ãã³ããç£è¦ãã
- æä½ã®æ¹åã«ã¤ãã¦ã¯
- C ã M ãæä½ãã (ãã®ã¨ãMã®ã¤ãã³ããçºç«ãããVãåæç»ããããï¼
- C ã V ãæä½ãã ï¼Mãä»ããªããããªæä½ããã¨ãã°è¡¨ç¤ºãã¦ããªãããæ¶ãã¨ãï¼
- ãã¼ã¿ã®åå¾ã®æ¹åã«ã¤ãã¦ã¯
- V ã M ã®ãã¼ã¿ãåå¾ããï¼æä½ã¯ããªãï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
- C ã V ã®ãã¼ã¿ãåå¾ãã
ãã¡ããããã¯åºæ¬ã§ãããããã®å¤å¥ãããããä¸ã®ä¸ã«ã¯ããã®ã ãã©ããã®ã¨ãã«ããã®MVCã®åºæ¬ã¯é ã«å ¥ã£ã¦ãã¦ãã©ãå¤å¥ããã¦ããã®ããæèããã¹ãã ã¨æããã¨ãããããããç解ããã« MV* ãã¬ã¼ã ã¯ã¼ã¯ä½¿ã£ã¦ã M 㨠V ãå¯çµåããæ°ãè¨ãããããã®ãåºæ¥ä¸ããã ãã§ãï¼ã¨ãã«JavaScript ã§ã¯ãããªããã¡ãªæãããããCocoa ã¨ãã¨éã£ã¦ View ã Controller ã«ã¤ãã³ãéç¥ããé¨åããã¬ã¼ã ã¯ã¼ã¯ã§æ ä¿ããã¦ãªãããããªï¼ï¼ãBackbone.js ã C ãæä¾ãã¦ãªãã¨ãããªãèªå㧠C ãä½ãã°ããããããã§ããªã人éã Mariotenette.js ã使ã£ãã¨ããã§çµå± MVC ãªæ§é ã«ã¯ãªããªãã®ã§ããã
å£ã ãã§èª¬æãã¦ãã¢ã¬ãªã®ã§ä¸åº¦ NDS ããªããã§ãã©ã¤ãã©ãªã使ããªãç´ ã® JavaScript ããå¦ã¶ MVC ã®åºæ¬ã®ãããããããããæ°æã¡ãããã
追è¨
Backbone.js ã® V 㯠V ã§ã¯ãªãã¨ãã話ããã³ã¡ã§åºã¦ãã¦ããªãã»ã©æããããããããããããªãã¨ä»åº¦ã¯ Marionnette.js ã®ææ³ã¨è¡çªãã¾ããâ¦â¦ããã®ãããã¯ã©ãèããã°ããã®ã ããï¼ãï¼
ãã¯ããåãã®MVCã¯ééãã¦ããªãã£ã in backbone.js
ãã³ã¡ã§ç解ãããã© backbone.js ã® View ã¯ããã P ã§ããããªã㨠M ãå¼ã¶ã®ããã¹ãªãããªã¨ããæãã§ããï¼ãåãã® MVC ã¯ééãã¦ããããããªããã©ãåãã® MVP ã¯ééãã¦ãªãã£ãï¼ãããï¼ã©ã£ã¡ãã£ã¦ãã㨠backbone.js ã® View ã£ã¦ããååãééãã¦ãï¼
MV* ã§å¤§äºãªã®ã¯ãè¦ãç®ã¨å®éã®ãã¼ã¿æä½ãåãé¢ãé¨åã§ãã£ã¦ãããããVãMãæä½ãããªãã«ãªãã®ã ããã©ãBackboneã«ã¨ã£ã¦ã®Vã¯Viewã§ã¯ãªãã¦ç´ ã®DOMã¨ã¬ã¡ã³ãã§ãããBackboneã®Viewã¯ã©ã¹ã¯DOMè¦ç´ ã¨ãã¼ã¿æä½ãåãé¢ãé¨åãæ å½ãã¦ããã¨èããã¨Viewï¼ã¨ããåã®Presenterï¼ããModelãæä½ããã®ãèªç¶ã§ããããããã¯Viewã¨ããååããããã®å®æ ãViewã§ãããåç´ã«DOMãwrap ããç¨åº¦ã®å½¹å²ãæå¾ ããã¦ããã¨åéããã¦ããã
ãã¨ãã¡ãã£ã¨è©±ãããããã¨ãããªãã ãã© backbone.js ã wrap ãã Marionnette.js ã¯ViewãView㧠C ã®å½¹å²ã¯ C ã«ããããããã£ã¦ããææ³ã«è¦ããããã®å ´åã¯backbone.jsã® View ã¯Vã¨ãã¦æ¯ãèãã¹ããªãã§ãããããããããã¯ããèªèãã¦ãããã£ã¦ãã¾ããã
ãªãã«ãããMV* ãªæ§é ã§ãªã«ããä½ãæã«ã¯ãç£è¦ã®æ¹åã¨ãã¼ã¿æä½ã®æ¹åããã¼ã¿åç §ã®æ¹åã常ã«æèãããã¨ãå¿ è¦ã§ããã¨ããã®ã¯è¨ããã¨æãããã©ãããã©ã¼ã ã«ãããã®æé©ãªããæ¹ã¯ç°ãªããã®ã§ããã©ãããã©ã¼ã ãã¨ã«ããããªMV* ãåºã¦ããã®ã§ããããã