çãã¬ãã¥ãŒ
äœãæ°ããããšãåŠã¶æè¯ã®æ¹æ³ã®1ã€ã¯ããã§ã«ç¥ã£ãŠããããšããã®äžã§ã©ã®ããã«äœ¿çšãããã確èªããããšã§ãã ãã®èšäºã¯ãèªè ã«ãã¶ã€ã³ããã¶ã€ã³ãã¿ãŒã³ãç解ããããšãæå³ããŠããŸããã OOPã®æŠå¿µãèšèšãã¿ãŒã³ãããã³ã¢ãŒããã¯ãã£ãã¿ãŒã³ã®åºæ¬çãªç解ãæäŸããŸãã ãã®èšäºã®ç®çã¯ãAngularJSãšãã®æžãããSPAã§ããŸããŸãªãœãããŠã§ã¢èšèšãšã¢ãŒããã¯ãã£ãã¿ãŒã³ãã©ã®ããã«äœ¿çšããããã説æããããšã§ãã
ã¯ããã«
ãã®èšäºã¯ãAngularJSãã¬ãŒã ã¯ãŒã¯ã®ç°¡åãªæŠèŠããå§ãŸããŸãã æŠèŠã§ã¯ãAngularJSã®äž»èŠã³ã³ããŒãã³ãïŒãã£ã¬ã¯ãã£ãããã£ã«ã¿ãŒãã³ã³ãããŒã©ãŒããµãŒãã¹ãã¹ã³ãŒãïŒã«ã€ããŠèª¬æããŸãã 2çªç®ã®ã»ã¯ã·ã§ã³ã§ã¯ããã¬ãŒã ã¯ãŒã¯å ã«å®è£ ãããããŸããŸãªèšèšãšã¢ãŒããã¯ãã£ãã¿ãŒã³ããªã¹ãããŠèª¬æããŸãã ãã³ãã¬ãŒãã¯ã䜿çšãããAngularJSã³ã³ããŒãã³ãããšã«ã°ã«ãŒãåãããŸãã ããã€ãã®ãã³ãã¬ãŒããè€æ°ã®ã³ã³ããŒãã³ãã§äœ¿çšãããŠããå Žåãããã瀺ãããŸãã
æåŸã®ã»ã¯ã·ã§ã³ã«ã¯ãAngularJSäžã«æ§ç¯ãããSPAã§äžè¬çã«äœ¿çšãããããã€ãã®ã¢ãŒããã¯ãã£ãã¿ãŒã³ãå«ãŸããŠããŸãã
AngularJSã®æŠèŠ
AngularJSã¯ãGoogleãéçºããJavaScript Webãã¬ãŒã ã¯ãŒã¯ã§ãã 圌ã¯CRUD SPAã®éçºã®ããã®åŒ·åºãªåºç€ãæäŸããã€ããã§ãã SPAã¯1åã ãããŒããããSPAã䜿çšãããšãã«ããŒãžããªããŒãããå¿ èŠã¯ãããŸããã ããã¯ãã¡ã€ã³ããŒãžãèªã¿èŸŒããšããŸãã¯ãªã³ããã³ãã§ããã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ãªãœãŒã¹ïŒããŒã¿ããã³ãã¬ãŒããã¹ã¯ãªãããã¹ã¿ã€ã«ïŒãèªã¿èŸŒãå¿ èŠãããããšãæå³ããŸãã ã»ãšãã©ã®CRUDã¢ããªã±ãŒã·ã§ã³ã«ã¯å ±éã®ç¹æ§ãšèŠä»¶ããããããAngularJSã¯ããã«æé©ãªã»ãããæäŸããäºå®ã§ãã AngularJSã®éèŠãªæ©èœã¯æ¬¡ã®ãšããã§ãã
責任ã®åé¢ã¯ãåAngularJSã¢ããªã±ãŒã·ã§ã³ã次ã®ãããªåå¥ã®ã³ã³ããŒãã³ãã«åå²ããããšã«ããå®çŸãããŸãã
ãããã®ã³ã³ããŒãã³ãã¯ãããŸããŸãªã¢ãžã¥ãŒã«å ã§ã°ã«ãŒãåããŠãããé«ãã¬ãã«ã®æœè±¡åãå®çŸã§ããŸãã åã³ã³ããŒãã³ãã«ã¯ãã¢ããªã±ãŒã·ã§ã³ããžãã¯ã®ç¹å®ã®éšåãå«ãŸããŸãã
ããŒã·ã£ã«ã¯HTMLè¡ã§ãã èŠçŽ ãŸãã¯ãã®å±æ§å ã«AngularJSåŒãå«ããããšãã§ããŸãã ä»ã®ãã¬ãŒã ã¯ãŒã¯ã«å¯ŸããAngularJSã®å©ç¹ã®1ã€ã¯ãAngularJSãã³ãã¬ãŒããHTMLã«å€æããå¿ èŠã®ããäžé圢åŒïŒmustache.jsãªã©ïŒã§ã¯ãªãããšã§ã
æåã«ãåSPAã¯Index.htmlãã¡ã€ã«ãããŒãããŸãã AngularJSã®å Žåããã®ãã¡ã€ã«ã«ã¯ãã¢ããªã±ãŒã·ã§ã³ãæ§æããã³èµ·åããäžé£ã®æšæºïŒããã³ããã§ã¯ãªãïŒHTMLå±æ§ãèŠçŽ ãããã³ã³ã¡ã³ããå«ãŸããŠããŸãã åãŠãŒã¶ãŒã¢ã¯ã·ã§ã³ã§ã¯ãä»ã®ããŒã·ã£ã«ïŒHTMLè¡ãŸãã¯HTMLããŒã¹ãå«ããã¡ã€ã«ïŒãããŒãããããããšãã°ãã¬ãŒã ã¯ãŒã¯ã«ãã£ãŠæäŸãããããŒã¿ãã€ã³ãã£ã³ã°ãä»ããŠã¢ããªã±ãŒã·ã§ã³ã®ç¶æ ãå€æŽããå¿ èŠããããŸãã
ããŒã·ã£ã«ã®äŸïŒ
AngularJSåŒãšãšãã«ãããŒã·ã£ã«ã¯ãŠãŒã¶ãŒãšå¯Ÿè©±ããããã«å®è¡ããå¿ èŠãããã¢ã¯ã·ã§ã³ã決å®ããŸãã äžèšã®äŸã§ã¯ãng-clickå±æ§ã®å€ã¯ãchangeFooïŒïŒã¡ãœãããçŸåšã®ã¹ã³ãŒãããåŒã³åºãããããšãæå³ããŸãã
AngularJSã®ã³ã³ãããŒã©ãŒã¯ãã¹ã³ãŒãã«ã¡ãœãããè¿œå ããããšã«ããããŠãŒã¶ãŒãšã¢ããªã±ãŒã·ã§ã³ã®å¯Ÿè©±ïŒããŠã¹ãããŒããŒããªã©ïŒã€ãã³ããåŠçã§ããäžè¬çãªæ©èœã§ãã ã³ã³ãããŒã©ã®ãã¹ãŠã®å€éšäŸåé¢ä¿ã¯ãAngularJSã®DIã¡ã«ããºã ã䜿çšããŠæäŸãããŸãã ã³ã³ãããŒã©ãŒã¯ãããŒã¿ãã¹ã³ãŒãã«è¿œå ããããšã«ãããã¢ãã«ãšããŒã·ã£ã«ã®çžäºäœçšãæ åœããŸãã ããã¯ãã¥ãŒã¢ãã«ãšããŠèŠãããšãã§ããŸãã
ããšãã°ãäžèšã®ã³ã³ãããŒã©ãŒãåã®ã»ã¯ã·ã§ã³ã«æ¥ç¶ãããšããŠãŒã¶ãŒã¯ããã€ãã®æ¹æ³ã§ã¢ããªã±ãŒã·ã§ã³ãšå¯Ÿè©±ã§ããŸãã
ãã¹ãŠã®ãŠãŒã¶ãŒèŠçŽ ãå±æ§ãã³ã¡ã³ãããŸãã¯ã¯ã©ã¹ã¯ãAngularJSãã£ã¬ã¯ãã£ãã«ããããšãã§ããŸãïŒäºåå®çŸ©ãããŠããå ŽåïŒã
AngularJSã§ã¯ãã¹ã³ãŒãã¯ããŒã·ã£ã«ã«ã¢ã¯ã»ã¹å¯èœãªJavaScriptãªããžã§ã¯ãã§ãã ã¹ã³ãŒãã«ã¯ãããªããã£ãããªããžã§ã¯ãããŸãã¯ã¡ãœãããªã©ã®ããŸããŸãªããããã£ãå«ããããšãã§ããŸãã ã¹ã³ãŒãã«è¿œå ããããã¹ãŠã®ã¡ãœããã¯ããã®ã¹ã³ãŒãã«é¢é£ä»ããããããŒã·ã£ã«å ã®AngularJSåŒã䜿çšããŠããŸãã¯ã¹ã³ãŒããžã®åç §ãæã€ã³ã³ããŒãã³ãã«ããçŽæ¥ã¡ãœããåŒã³åºãã«ãã£ãŠåŒã³åºãããšãã§ããŸãã é©åãªãã£ã¬ã¯ãã£ãã䜿çšããŠãããŒã¿ãã¹ã³ãŒãã«è¿œå ããããã¥ãŒã«é¢é£ä»ããããšãã§ãããããã¹ã³ãŒãããããã£ã®ãã¹ãŠã®å€æŽããã¥ãŒã«åæ ããããã¥ãŒã®ãã¹ãŠã®å€æŽãã¹ã³ãŒãã«è¡šç€ºãããŸãã
AngularJSã¢ããªã±ãŒã·ã§ã³ã®ã¹ã³ãŒãã®ãã1ã€ã®éèŠãªç¹åŸŽã¯ããããã¿ã€ãã®ç¶æ¿ã¡ã«ããºã ãä»ããŠãªã³ã¯ãããŠããããšã§ãïŒåé¢ãããã¹ã³ãŒããé€ãïŒã ãããã£ãŠããã¹ãŠã®åã¹ã³ãŒãã¯ãã®èŠªã®ã¡ãœããã䜿çšã§ããŸãããããã¯çŽæ¥ãŸãã¯éæ¥ãããã¿ã€ãã®ããããã£ã§ããããã§ãã
ã¹ã³ãŒãã®ç¶æ¿ã次ã®äŸã«ç€ºããŸãã
ChildCtrlã®ã¹ã³ãŒãã¯divïŒåã«é¢é£ä»ããããŠããŸãããChildCtrlã®ã¹ã³ãŒãã¯BaseCtrlã«ãã¹ããããŠãããããBaseCtrlã®ãã¹ãŠã®ã¡ãœããã¯ãããã¿ã€ãã®ç¶æ¿ã䜿çšããŠChildCtrlã§äœ¿çšã§ãããããã£ãŠfooã¡ãœããã¯ãã¿ã³ïŒparent-methodãã¯ãªãã¯ãããšäœ¿çšå¯èœã«ãªããŸãã
AngularJSã®ãã£ã¬ã¯ãã£ãã¯ããã¹ãŠã®DOMæäœãå®è¡ããå Žæã§ãã ååãšããŠãã³ã³ãããŒã©ãŒã®DOMã䜿çšããæäœãããå Žåã¯ãã³ã³ãããŒã©ãŒã®DOMã䜿çšããæäœãæé€ããæ°ãããã£ã¬ã¯ãã£ããŸãã¯ãªãã¡ã¯ã¿ãªã³ã°ãäœæããå¿ èŠããããŸãã æãåçŽãªã±ãŒã¹ã§ã¯ããã£ã¬ã¯ãã£ãã«ã¯ããã£ã¬ã¯ãã£ãã®ããžãã¯ãå«ãpostLinké¢æ°ã®ååãšå®çŸ©ããããŸãã ããè€éãªå Žåããã£ã¬ã¯ãã£ãã«ã¯æ¬¡ã®ãããªå€ãã®ããããã£ãå«ããããšãã§ããŸãã
ãã£ã¬ã¯ãã£ãã¯ããŒã·ã£ã«ã§äœ¿çšã§ããŸããäŸïŒ
äžèšã®äŸã§ã¯ã<alert-button> </ alert-button>ã¿ã°ã¯ãã¿ã³èŠçŽ ã«çœ®ãæãããããã¿ã³ãã¯ãªãã¯ãããšãããã¹ã42ãå«ãèŠåã衚瀺ãããŸãã
AngularJSã®ãã£ã«ã¿ãŒã¯ãããŒã¿ã®ãã©ãŒãããã«å¿ èŠãªããžãã¯ãã«ãã»ã«åããŸãã éåžžããã£ã«ã¿ãŒã¯ããŒã·ã£ã«å ã§äœ¿çšãããŸãããDIãä»ããŠã³ã³ãããŒã©ãŒããã£ã¬ã¯ãã£ãããµãŒãã¹ããŸãã¯ãã®ä»ã®ãã£ã«ã¿ãŒã§ã䜿çšã§ããŸãã
æååã倧æåã«å€æããç°¡åãªãã£ã«ã¿ãŒã®äŸã次ã«ç€ºããŸãã
ããŒã·ã£ã«å ã§ã¯ãUnixãã€ãã³ã°æ§æïŒUnixã®ãã€ãã³ã°ïŒã䜿çšããŠãã£ã«ã¿ãŒã䜿çšã§ããŸãã
ã³ã³ãããŒã©ãŒå ã§ã¯ããã£ã«ã¿ãŒã¯æ¬¡ã®ããã«äœ¿çšã§ããŸãã
äžèšã®ã³ã³ããŒãã³ãã«é©çšãããªãããžãã¯ã®éšåã¯ããµãŒãã¹ã«é 眮ããå¿ èŠããããŸãã éåžžããµãŒãã¹ã¯ããžãã¯ãäžå€ããžãã¯ãXHRãWebSocketãªã©ã®ç¹å®ã®é åãã«ãã»ã«åããŸããã¢ããªã±ãŒã·ã§ã³ã®ã³ã³ãããŒã©ãŒããåããªãããããå Žåãç¹°ãè¿ãããã³ãŒãããµãŒãã¹ã«éä¿¡ããå¿ èŠããããŸãã
ãµãŒãã¹ã¯ãDIããµããŒãããã³ã³ããŒãã³ãïŒã³ã³ãããŒã©ãŒããã£ã«ã¿ãŒããã£ã¬ã¯ãã£ãããã®ä»ã®ãµãŒãã¹ïŒã«è¿œå ã§ããŸãã
AngularJSã®æŠèŠ
AngularJSã¯ãGoogleãéçºããJavaScript Webãã¬ãŒã ã¯ãŒã¯ã§ãã 圌ã¯CRUD SPAã®éçºã®ããã®åŒ·åºãªåºç€ãæäŸããã€ããã§ãã SPAã¯1åã ãããŒããããSPAã䜿çšãããšãã«ããŒãžããªããŒãããå¿ èŠã¯ãããŸããã ããã¯ãã¡ã€ã³ããŒãžãèªã¿èŸŒããšããŸãã¯ãªã³ããã³ãã§ããã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ãªãœãŒã¹ïŒããŒã¿ããã³ãã¬ãŒããã¹ã¯ãªãããã¹ã¿ã€ã«ïŒãèªã¿èŸŒãå¿ èŠãããããšãæå³ããŸãã ã»ãšãã©ã®CRUDã¢ããªã±ãŒã·ã§ã³ã«ã¯å ±éã®ç¹æ§ãšèŠä»¶ããããããAngularJSã¯ããã«æé©ãªã»ãããæäŸããäºå®ã§ãã AngularJSã®éèŠãªæ©èœã¯æ¬¡ã®ãšããã§ãã
- åæ¹åã®ããŒã¿ãã€ã³ãã£ã³ã°
- äŸåæ§æ³šå ¥ãDI ïŒäŸåæ§æ³šå ¥ïŒ
- é¢å¿ã®åé¢
- ãã¹ã¿ããªãã£
- æœè±¡å
責任ã®åé¢ã¯ãåAngularJSã¢ããªã±ãŒã·ã§ã³ã次ã®ãããªåå¥ã®ã³ã³ããŒãã³ãã«åå²ããããšã«ããå®çŸãããŸãã
- ããŒã·ã£ã«
- ã³ã³ãããŒã©ãŒ
- æ什
- ãµãŒãã¹
- ãã£ã«ã¿ãŒ
ãããã®ã³ã³ããŒãã³ãã¯ãããŸããŸãªã¢ãžã¥ãŒã«å ã§ã°ã«ãŒãåããŠãããé«ãã¬ãã«ã®æœè±¡åãå®çŸã§ããŸãã åã³ã³ããŒãã³ãã«ã¯ãã¢ããªã±ãŒã·ã§ã³ããžãã¯ã®ç¹å®ã®éšåãå«ãŸããŸãã
ããŒã·ã£ã«
ããŒã·ã£ã«ã¯HTMLè¡ã§ãã èŠçŽ ãŸãã¯ãã®å±æ§å ã«AngularJSåŒãå«ããããšãã§ããŸãã ä»ã®ãã¬ãŒã ã¯ãŒã¯ã«å¯ŸããAngularJSã®å©ç¹ã®1ã€ã¯ãAngularJSãã³ãã¬ãŒããHTMLã«å€æããå¿ èŠã®ããäžé圢åŒïŒmustache.jsãªã©ïŒã§ã¯ãªãããšã§ã
æåã«ãåSPAã¯Index.htmlãã¡ã€ã«ãããŒãããŸãã AngularJSã®å Žåããã®ãã¡ã€ã«ã«ã¯ãã¢ããªã±ãŒã·ã§ã³ãæ§æããã³èµ·åããäžé£ã®æšæºïŒããã³ããã§ã¯ãªãïŒHTMLå±æ§ãèŠçŽ ãããã³ã³ã¡ã³ããå«ãŸããŠããŸãã åãŠãŒã¶ãŒã¢ã¯ã·ã§ã³ã§ã¯ãä»ã®ããŒã·ã£ã«ïŒHTMLè¡ãŸãã¯HTMLããŒã¹ãå«ããã¡ã€ã«ïŒãããŒãããããããšãã°ãã¬ãŒã ã¯ãŒã¯ã«ãã£ãŠæäŸãããããŒã¿ãã€ã³ãã£ã³ã°ãä»ããŠã¢ããªã±ãŒã·ã§ã³ã®ç¶æ ãå€æŽããå¿ èŠããããŸãã
ããŒã·ã£ã«ã®äŸïŒ
<html ng-app> <!-- Body tag augmented with ngController directive --> <body ng-controller="MyController"> <input ng-model="foo" value="bar"> <!-- Button tag with ng-click directive, and string expression 'buttonText' wrapped in "{{ }}" markup --> <button ng-click="changeFoo()">{{buttonText}}</button> <script src="angular.js"></script> </body> </html>
AngularJSåŒãšãšãã«ãããŒã·ã£ã«ã¯ãŠãŒã¶ãŒãšå¯Ÿè©±ããããã«å®è¡ããå¿ èŠãããã¢ã¯ã·ã§ã³ã決å®ããŸãã äžèšã®äŸã§ã¯ãng-clickå±æ§ã®å€ã¯ãchangeFooïŒïŒã¡ãœãããçŸåšã®ã¹ã³ãŒãããåŒã³åºãããããšãæå³ããŸãã
ã³ã³ãããŒã©ãŒ
AngularJSã®ã³ã³ãããŒã©ãŒã¯ãã¹ã³ãŒãã«ã¡ãœãããè¿œå ããããšã«ããããŠãŒã¶ãŒãšã¢ããªã±ãŒã·ã§ã³ã®å¯Ÿè©±ïŒããŠã¹ãããŒããŒããªã©ïŒã€ãã³ããåŠçã§ããäžè¬çãªæ©èœã§ãã ã³ã³ãããŒã©ã®ãã¹ãŠã®å€éšäŸåé¢ä¿ã¯ãAngularJSã®DIã¡ã«ããºã ã䜿çšããŠæäŸãããŸãã ã³ã³ãããŒã©ãŒã¯ãããŒã¿ãã¹ã³ãŒãã«è¿œå ããããšã«ãããã¢ãã«ãšããŒã·ã£ã«ã®çžäºäœçšãæ åœããŸãã ããã¯ãã¥ãŒã¢ãã«ãšããŠèŠãããšãã§ããŸãã
function MyController($scope) { $scope.buttonText = 'Click me to change foo!'; $scope.foo = 42; $scope.changeFoo = function () { $scope.foo += 1; alert('Foo changed'); }; }
ããšãã°ãäžèšã®ã³ã³ãããŒã©ãŒãåã®ã»ã¯ã·ã§ã³ã«æ¥ç¶ãããšããŠãŒã¶ãŒã¯ããã€ãã®æ¹æ³ã§ã¢ããªã±ãŒã·ã§ã³ãšå¯Ÿè©±ã§ããŸãã
- å ¥åãã£ãŒã«ãã«ããŒã¿ãå ¥åããŠãfooããå€æŽããŸãã ããã¯ãåæ¹åã®ããŒã¿ãã€ã³ãã£ã³ã°ã«ããããfooãå€ã«ããã«åœ±é¿ããŸãã
- ãClick me to fooïŒããšããååã®ãã¿ã³ãã¯ãªãã¯ããŠããfooãã®å€ãå€æŽããŸã
ãã¹ãŠã®ãŠãŒã¶ãŒèŠçŽ ãå±æ§ãã³ã¡ã³ãããŸãã¯ã¯ã©ã¹ã¯ãAngularJSãã£ã¬ã¯ãã£ãã«ããããšãã§ããŸãïŒäºåå®çŸ©ãããŠããå ŽåïŒã
ç¯å²
AngularJSã§ã¯ãã¹ã³ãŒãã¯ããŒã·ã£ã«ã«ã¢ã¯ã»ã¹å¯èœãªJavaScriptãªããžã§ã¯ãã§ãã ã¹ã³ãŒãã«ã¯ãããªããã£ãããªããžã§ã¯ãããŸãã¯ã¡ãœãããªã©ã®ããŸããŸãªããããã£ãå«ããããšãã§ããŸãã ã¹ã³ãŒãã«è¿œå ããããã¹ãŠã®ã¡ãœããã¯ããã®ã¹ã³ãŒãã«é¢é£ä»ããããããŒã·ã£ã«å ã®AngularJSåŒã䜿çšããŠããŸãã¯ã¹ã³ãŒããžã®åç §ãæã€ã³ã³ããŒãã³ãã«ããçŽæ¥ã¡ãœããåŒã³åºãã«ãã£ãŠåŒã³åºãããšãã§ããŸãã é©åãªãã£ã¬ã¯ãã£ãã䜿çšããŠãããŒã¿ãã¹ã³ãŒãã«è¿œå ããããã¥ãŒã«é¢é£ä»ããããšãã§ãããããã¹ã³ãŒãããããã£ã®ãã¹ãŠã®å€æŽããã¥ãŒã«åæ ããããã¥ãŒã®ãã¹ãŠã®å€æŽãã¹ã³ãŒãã«è¡šç€ºãããŸãã
AngularJSã¢ããªã±ãŒã·ã§ã³ã®ã¹ã³ãŒãã®ãã1ã€ã®éèŠãªç¹åŸŽã¯ããããã¿ã€ãã®ç¶æ¿ã¡ã«ããºã ãä»ããŠãªã³ã¯ãããŠããããšã§ãïŒåé¢ãããã¹ã³ãŒããé€ãïŒã ãããã£ãŠããã¹ãŠã®åã¹ã³ãŒãã¯ãã®èŠªã®ã¡ãœããã䜿çšã§ããŸãããããã¯çŽæ¥ãŸãã¯éæ¥ãããã¿ã€ãã®ããããã£ã§ããããã§ãã
ã¹ã³ãŒãã®ç¶æ¿ã次ã®äŸã«ç€ºããŸãã
<div ng-controller="BaseCtrl"> <div id="child" ng-controller="ChildCtrl"> <button id="parent-method" ng-click="foo()">Parent method</button> <button ng-click="bar()">Child method</button> </div> </div>
function BaseCtrl($scope) { $scope.foo = function () { alert('Base foo'); }; } function ChildCtrl($scope) { $scope.bar = function () { alert('Child bar'); }; }
ChildCtrlã®ã¹ã³ãŒãã¯divïŒåã«é¢é£ä»ããããŠããŸãããChildCtrlã®ã¹ã³ãŒãã¯BaseCtrlã«ãã¹ããããŠãããããBaseCtrlã®ãã¹ãŠã®ã¡ãœããã¯ãããã¿ã€ãã®ç¶æ¿ã䜿çšããŠChildCtrlã§äœ¿çšã§ãããããã£ãŠfooã¡ãœããã¯ãã¿ã³ïŒparent-methodãã¯ãªãã¯ãããšäœ¿çšå¯èœã«ãªããŸãã
æ什
AngularJSã®ãã£ã¬ã¯ãã£ãã¯ããã¹ãŠã®DOMæäœãå®è¡ããå Žæã§ãã ååãšããŠãã³ã³ãããŒã©ãŒã®DOMã䜿çšããæäœãããå Žåã¯ãã³ã³ãããŒã©ãŒã®DOMã䜿çšããæäœãæé€ããæ°ãããã£ã¬ã¯ãã£ããŸãã¯ãªãã¡ã¯ã¿ãªã³ã°ãäœæããå¿ èŠããããŸãã æãåçŽãªã±ãŒã¹ã§ã¯ããã£ã¬ã¯ãã£ãã«ã¯ããã£ã¬ã¯ãã£ãã®ããžãã¯ãå«ãpostLinké¢æ°ã®ååãšå®çŸ©ããããŸãã ããè€éãªå Žåããã£ã¬ã¯ãã£ãã«ã¯æ¬¡ã®ãããªå€ãã®ããããã£ãå«ããããšãã§ããŸãã
- ãã³ãã¬ãŒã
- ã³ã³ãã€ã«æ©èœ
- ãªã³ã¯æ©èœ
- ãªã©
ãã£ã¬ã¯ãã£ãã¯ããŒã·ã£ã«ã§äœ¿çšã§ããŸããäŸïŒ
myModule.directive('alertButton', function () { return { template: '<button ng-transclude></button>', scope: { content: '@' }, replace: true, restrict: 'E', transclude: true, link: function (scope, el) { el.click(function () { alert(scope.content); }); } }; });
<alert-button content="42">Click me</alert-button>
äžèšã®äŸã§ã¯ã<alert-button> </ alert-button>ã¿ã°ã¯ãã¿ã³èŠçŽ ã«çœ®ãæãããããã¿ã³ãã¯ãªãã¯ãããšãããã¹ã42ãå«ãèŠåã衚瀺ãããŸãã
ãã£ã«ã¿ãŒ
AngularJSã®ãã£ã«ã¿ãŒã¯ãããŒã¿ã®ãã©ãŒãããã«å¿ èŠãªããžãã¯ãã«ãã»ã«åããŸãã éåžžããã£ã«ã¿ãŒã¯ããŒã·ã£ã«å ã§äœ¿çšãããŸãããDIãä»ããŠã³ã³ãããŒã©ãŒããã£ã¬ã¯ãã£ãããµãŒãã¹ããŸãã¯ãã®ä»ã®ãã£ã«ã¿ãŒã§ã䜿çšã§ããŸãã
æååã倧æåã«å€æããç°¡åãªãã£ã«ã¿ãŒã®äŸã次ã«ç€ºããŸãã
myModule.filter('uppercase', function () { return function (str) { return (str || '').toUpperCase(); }; });
ããŒã·ã£ã«å ã§ã¯ãUnixãã€ãã³ã°æ§æïŒUnixã®ãã€ãã³ã°ïŒã䜿çšããŠãã£ã«ã¿ãŒã䜿çšã§ããŸãã
<div>{{ name | uppercase }}</div>
ã³ã³ãããŒã©ãŒå ã§ã¯ããã£ã«ã¿ãŒã¯æ¬¡ã®ããã«äœ¿çšã§ããŸãã
function MyCtrl(uppercaseFilter) { $scope.name = uppercaseFilter('foo'); //FOO }
ãµãŒãã¹
äžèšã®ã³ã³ããŒãã³ãã«é©çšãããªãããžãã¯ã®éšåã¯ããµãŒãã¹ã«é 眮ããå¿ èŠããããŸãã éåžžããµãŒãã¹ã¯ããžãã¯ãäžå€ããžãã¯ãXHRãWebSocketãªã©ã®ç¹å®ã®é åãã«ãã»ã«åããŸããã¢ããªã±ãŒã·ã§ã³ã®ã³ã³ãããŒã©ãŒããåããªãããããå Žåãç¹°ãè¿ãããã³ãŒãããµãŒãã¹ã«éä¿¡ããå¿ èŠããããŸãã
myModule.service('Developer', function () { this.name = 'Foo'; this.motherLanguage = 'JavaScript'; this.live = function () { while (true) { this.code(); } }; });
ãµãŒãã¹ã¯ãDIããµããŒãããã³ã³ããŒãã³ãïŒã³ã³ãããŒã©ãŒããã£ã«ã¿ãŒããã£ã¬ã¯ãã£ãããã®ä»ã®ãµãŒãã¹ïŒã«è¿œå ã§ããŸãã
function MyCtrl(developer) { var developer = new Developer(); developer.live(); }
AngularJSãã¿ãŒã³
次ã®2ã€ã®ã»ã¯ã·ã§ã³ã§ã¯ãAngularJSã³ã³ããŒãã³ãã§åŸæ¥ã®èšèšããã³ã¢ãŒããã¯ãã£ãã¿ãŒã³ãã©ã®ããã«äœ¿çšãããããèŠãŠãããŸãã
æåŸã®ç« ã§ã¯ãAngularJSã§ïŒã ãã§ãªãïŒSPAãéçºãããšãã«ãã䜿çšãããããã€ãã®ã¢ãŒããã¯ãã£ãã¿ãŒã³ãèŠãŠãããŸãã
ãµãŒãã¹
ã·ã³ã°ã«ãã³
ã·ã³ã°ã«ãã³ã¯ãã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã®äœæãåäžã®ãªããžã§ã¯ãã«å¶éããèšèšãã¿ãŒã³ã§ãã ããã¯ãã·ã¹ãã å šäœã§ã¢ã¯ã·ã§ã³ã調æŽããå¿ èŠãããå Žåã«åœ¹ç«ã¡ãŸãã ãã®æŠå¿µã¯ããªããžã§ã¯ãã1ã€ãããªãå Žåããã€ã³ã¹ã¿ã³ã¹ãç¹å®ã®æ°ã®ãªããžã§ã¯ãã«ãã£ãŠå¶éãããŠããå Žåã«ãããå¹ççã«åäœããã·ã¹ãã ã«é©ããŠããŸãã
UMLå³ã¯ãã·ã³ã°ã«ãã³ãã¿ãŒã³ã瀺ããŠããŸãã
ã³ã³ããŒãã³ãã«äŸåé¢ä¿ãå¿ èŠãªå ŽåãAngularJSã¯æ¬¡ã®ã¢ã«ãŽãªãºã ã䜿çšããŠããã解決ããŸãã
- äŸåé¢ä¿ã®ååãåããåå¥ã¯ããŒãžã£ã§å®çŸ©ãããŠããããã·ã¥ãããã§æ€çŽ¢ãè¡ããŸãã ïŒãããã£ãŠããã©ã€ããŒãã¹ã³ãŒãããããŸãïŒ
- äŸåé¢ä¿ãèŠã€ãã£ãå ŽåãAngularJSã¯ãããã³ã³ããŒãã³ããã©ã¡ãŒã¿ãŒãšããŠæž¡ããŸã
- äŸåé¢ä¿ãèŠã€ãããªãå ŽåïŒ
- AngularJSã¯ããã¡ã¯ããªã¡ãœãããŸãã¯ãã®ãããã€ããŒïŒã€ãŸãã$ getïŒãåŒã³åºããŠäœæããŸãã äŸåé¢ä¿ãäœæããã«ã¯ãç¹å®ã®äŸåé¢ä¿ã®ãã¹ãŠã®äŸåé¢ä¿ã決å®ããããã«ãåãã¢ã«ãŽãªãºã ã䜿çšããååž°åŒã³åºããå¿ èŠã«ãªãå Žåãããããšã«æ³šæããŠãã ããã ãã®ããã»ã¹ã¯ã埪ç°äŸåã«ã€ãªããå¯èœæ§ããããŸãã
- AngularJSã¯ãäžèšã®ããã·ã¥ãããå ã«ãã£ãã·ã¥ããŸãã
- AngularJSã¯ãæå®ãããã³ã³ããŒãã³ãã«ãã©ã¡ãŒã¿ãŒãšããŠæž¡ããŸãã
getServiceãå®è£ ããAngularJSã®ãœãŒã¹ã³ãŒããããèŠãŠãã ããã
function getService(serviceName) { if (cache.hasOwnProperty(serviceName)) { if (cache[serviceName] === INSTANTIATING) { throw $injectorMinErr('cdep', 'Circular dependency found: {0}', path.join(' <- ')); } return cache[serviceName]; } else { try { path.unshift(serviceName); cache[serviceName] = INSTANTIATING; return cache[serviceName] = factory(serviceName); } catch (err) { if (cache[serviceName] === INSTANTIATING) { delete cache[serviceName]; } throw err; } finally { path.shift(); } } }
ãµãŒãã¹ã¯äžåºŠããäœæãããªããããåãµãŒãã¹ãã·ã³ã°ã«ãã³ã§ãããšæ³åããŠãã ããã ãã£ãã·ã¥ã¯ã·ã³ã°ã«ãã³ãããŒãžã£ãŒãšèããããšãã§ããŸãã ãŸãããã®å®è£ ã¯UMLå³ã«ç€ºãããŠããå®è£ ãšã¯è¥å¹²ç°ãªããŸããã³ã³ã¹ãã©ã¯ã¿å ã§ã·ã³ã°ã«ãã³ãžã®éçãªãã©ã€ããŒããªã³ã¯ãäœæãã代ããã«ãã·ã³ã°ã«ãã³ãããŒãžã£å ã§ãªã³ã¯ãä¿åããããã§ãã
ãããã£ãŠããµãŒãã¹ã¯å®éã«ã¯ã·ã³ã°ã«ãã³ã§ãããã·ã³ã°ã«ãã³ãã³ãã¬ãŒããä»ããŠå®è£ ãããŠããªããããæšæºå®è£ ãããããã€ãã®å©ç¹ããããŸãã
- ã³ãŒãã®ãã¹ã容ææ§ãæ¹å
- ã·ã³ã°ã«ãã³ãªããžã§ã¯ãã®äœæãå¶åŸ¡ã§ããŸãïŒãã®å Žåã IoCã³ã³ãããŒã¯é 延åŒã³åºãã䜿çšããŠãªããžã§ã¯ãã®äœæãå¶åŸ¡ããŸããïŒ
ãã®ãããã¯ã®è©³çŽ°ã«ã€ããŠã¯ãGoogle Testingããã°ã®Misko Heveryã®èšäºãã芧ãã ããã
å·¥å Žæ¹åŒ
Factory MethodïŒVirtual ConstructorãšãåŒã°ããïŒã¯ãã¯ã©ã¹ãã€ã³ã¹ã¿ã³ã¹åããããã®ã€ã³ã¿ãŒãã§ãŒã¹ããµãã¯ã©ã¹ã«æäŸããæ±çšèšèšãã¿ãŒã³ã§ãã äœææã«ãçžç¶äººã¯äœæããã¯ã©ã¹ã決å®ã§ããŸãã ã€ãŸãããã¡ã¯ããªã¯ãªããžã§ã¯ãã®äœæã芪ã¯ã©ã¹ã®ç¶æ¿è ã«å§ä»»ããŸãã ããã«ãããããã°ã©ã ã³ãŒãã§ç¹å®ã®ã¯ã©ã¹ã䜿çšããã®ã§ã¯ãªããããé«ãã¬ãã«ã§æœè±¡ãªããžã§ã¯ããæäœã§ããŸãã
次ã®ã¹ãããããèŠãŠã¿ãŸãããã
myModule.config(function ($provide) { $provide.provider('foo', function () { var baz = 42; return { //Factory method $get: function (bar) { var baz = bar.baz(); return { baz: baz }; } }; }); });
ããã§ã¯ãconfigã³ãŒã«ããã¯é¢æ°ã䜿çšããŠãæ°ããããããã€ããŒããå®çŸ©ããŠããŸãã ããããã€ããŒãã¯ã$ getã¡ãœãããæã€ãªããžã§ã¯ãã§ãã JavaScriptã«ã¯ã€ã³ã¿ãŒãã§ãŒã¹ããªããèšèªã¯ããã¯ã¿ã€ãã³ã°ã䜿çšããããããããã€ããŒã§ãã¡ã¯ããªã¡ãœããã$ getãšããŠäœ¿çšããããšã«åæããŸããã
åãµãŒãã¹ããã£ã«ã¿ãŒããã£ã¬ã¯ãã£ããããã³ã³ã³ãããŒã©ãŒã«ã¯ãããã€ããŒïŒã€ãŸãã$ get factoryã¡ãœãããæã€ãªããžã§ã¯ãïŒããããã³ã³ããŒãã³ãã®ã€ã³ã¹ã¿ã³ã¹åãæ åœããŸãã
AngularJSã®å®è£ ãããå°ãæ·±ãæãäžããããšãã§ããŸãã
//... createInternalInjector(instanceCache, function(servicename) { var provider = providerInjector.get(servicename + providerSuffix); return instanceInjector.invoke(provider.$get, provider, undefined, servicename); }, strictDi)); //... function invoke(fn, self, locals, serviceName){ if (typeof locals === 'string') { serviceName = locals; locals = null; } var args = [], $inject = annotate(fn, strictDi, serviceName), length, i, key; for(i = 0, length = $inject.length; i < length; i++) { key = $inject[i]; if (typeof key !== 'string') { throw $injectorMinErr('itkn', 'Incorrect injection token! Expected service name as string, got {0}', key); } args.push(locals && locals.hasOwnProperty(key) ? locals[key] : getService(key)); } if (!fn.$inject) { // this means that we must be an array. fn = fn[length]; } return fn.apply(self, args); }
ãã®äŸã§ã¯ã$ getã¡ãœããã®å®éã®äœ¿çšæ¹æ³ã確èªã§ããŸãã
instanceInjector.invoke(provider.$get, provider, undefined, servicename)
äžèšã®ãã©ã°ã¡ã³ãã§ã¯ãinvokeã¡ãœãããåŒã³åºããããµãŒãã¹ã®ãã¡ã¯ããªã¡ãœããïŒ$ getïŒãæåã®åŒæ°ãšããŠæž¡ãããŸãã invokeã¡ãœããå ã§ã泚éé¢æ°ãåŒã³åºããããã¡ã¯ããªã¡ãœãããæåã®åŒæ°ãšããŠæž¡ãããŸãã 泚éé¢æ°ã¯ãAngularJS DIã¡ã«ããºã ïŒåè¿°ïŒãä»ããŠãã¹ãŠã®äŸåé¢ä¿ã解決ããŸãã ãã¹ãŠã®äŸåé¢ä¿ã解決ããåŸããã¡ã¯ããªã¡ãœãããåŒã³åºãããŸãã
fn.apply(self, args).
äžèšã®UMLãã€ã¢ã°ã©ã ã®èŠ³ç¹ããèšãã°ãCreatorãåŒã³åºãããšãã§ããŸããCreatorã¯ããã¡ã¯ããªã¡ãœãããä»ããŠãConcreteCreatorããåŒã³åºãããProductããäœæããŸãã
ãã®å Žåãã€ã³ã¹ã¿ã³ã¹ã®éæ¥çãªäœæã䜿çšãããããããã¡ã¯ããªã¡ãœãããã³ãã¬ãŒãã䜿çšãããšããã€ãã®å©ç¹ãåŸãããŸãã ãããã£ãŠããã¬ãŒã ã¯ãŒã¯ã¯ãæ°ããã³ã³ããŒãã³ããäœæããããã®ã¬ã€ã¢ãŠã/ãã³ãã¬ãŒãã«åœ±é¿ããŸãã
- ããã¯ãã³ã³ããŒãã³ããäœæããå¿ èŠãããå Žåã«æé©ãªç¬éã§ã
- ãã¹ãŠã®ã³ã³ããŒãã³ãã®äŸåé¢ä¿ãèš±å¯ãã
- ã³ã³ããŒãã³ãã®èš±å¯ãããã€ã³ã¹ã¿ã³ã¹ã®æ°ãå¶åŸ¡ããŸãïŒãµãŒãã¹ããã³ãã£ã«ã¿ãŒã®1ã€ã§ãããã³ã³ãããŒã©ãŒã®å€ãã¯ïŒ
ãã³ã¬ãŒã¿
ãã³ã¬ãŒã¿ã¯ããªããžã§ã¯ãã«åäœãåçã«è¿œå ããããã«èšèšãããæ§é èšèšãã³ãã¬ãŒãã§ãã Decoratorãã¿ãŒã³ã¯ãæ©èœãæ¡åŒµããããã®ãµãã¯ã©ã¹åã®æè»ãªä»£æ¿æ段ãæäŸããŸãã
ããã«äœ¿çšã§ããAngularJSã«ã¯ãæ¢åã®ãµãŒãã¹ã®æ©èœãæ¡åŒµããã³/ãŸãã¯æ¡åŒµãããªãã·ã§ã³ãçšæãããŠããŸãã ãã³ã¬ãŒã¿ãŸãã¯$æäŸã¡ãœããã䜿çšããŠãèªåãŸãã¯ãµãŒãããŒãã£ã©ã€ãã©ãªããæ¢ã«å®çŸ©ãããŠãããµãŒãã¹ã®ã©ãããŒãäœæã§ããŸãã
myModule.controller('MainCtrl', function (foo) { foo.bar(); }); myModule.factory('foo', function () { return { bar: function () { console.log('I\'m bar'); }, baz: function () { console.log('I\'m baz'); } }; }); myModule.config(function ($provide) { $provide.decorator('foo', function ($delegate) { var barBackup = $delegate.bar; $delegate.bar = function () { console.log('Decorated'); barBackup.apply($delegate, arguments); }; return $delegate; }); });
äžèšã®äŸã§ã¯ããfooããšããååã®æ°ãããµãŒãã¹ãå®çŸ©ããŠããŸãã $ provide.decoratorã¡ãœããã¯ã³ãŒã«ããã¯é¢æ°ãconfigãã§åŒã³åºããããã³ã¬ãŒã·ã§ã³ãããµãŒãã¹ã®ååãæåã®åŒæ°ãšããŠæž¡ãããé¢æ°ã2çªç®ã®åŒæ°ãšããŠæž¡ãããå®éã«ãã³ã¬ãŒã¿ãå®è£ ãããŸãã $ããªã²ãŒãã¯ãå ã®ãµãŒãã¹fooãžã®ãªã³ã¯ãä¿åããŸãã barã¡ãœããããªãŒããŒã©ã€ãããŠããµãŒãã¹ã食ããŸãã å®éãè£ é£Ÿã¯ãå¥ã®console.logç¶æ -console.logïŒ 'Decorated'ïŒãå«ããããšã«ãããåã«barã®æ¡åŒµã§ãããé©åãªã³ã³ããã¹ãã§å ã®barã¡ãœãããåŒã³åºããŸãã
ãã³ãã¬ãŒãã䜿çšãããšããµãŒãããŒãã£ãäœæãããµãŒãã¹ã®æ©èœãå€æŽããå¿ èŠãããå Žåã«ç¹ã«äŸ¿å©ã§ãã å€æ°ã®ãã³ã¬ãŒã¿ãå¿ èŠãªå ŽåïŒã¡ãœãããæ¿èªãç»é²ãªã©ã®ããã©ãŒãã³ã¹ã枬å®ããå Žåãªã©ïŒãå€ãã®éè€ã³ãŒããšDRYååã®éåãçºçããå¯èœæ§ããããŸãã ãã®ãããªå Žåã ã¢ã¹ãã¯ãæåããã°ã©ãã³ã°ã䜿çšããããšããå§ãããŸãã AngularJSã®AOPãã¬ãŒã ã¯ãŒã¯ã¯ã github.com / mgechev / angular-aopã«ãããŸãã
æ£é¢
ãã¡ãµãŒããã³ãã¬ãŒã-1ã€ã®ãªããžã§ã¯ããžã®ãã¹ãŠã®å¯èœãªå€éšåŒã³åºããæžããããããã察å¿ããã·ã¹ãã ãªããžã§ã¯ãã«å§ä»»ããããšã«ãããã·ã¹ãã ã®è€éããé ãããšãã§ããæ§é èšèšãã³ãã¬ãŒãã
ãã¡ãµãŒãã¯ïŒ
- ãã¡ãµãŒãã«ã¯äžè¬çãªã¿ã¹ã¯ãå®è¡ããããã®ããé©åãªæ¹æ³ããããããã©ã€ãã©ãªã®äœ¿çšãç解ããã¹ãã容æã«ãããã
- åãçç±ã§ãã©ã€ãã©ãªãèªã¿ãããããŸã
- ã»ãšãã©ã®ã³ãŒãã¯ãã¡ãµãŒãã䜿çšããŠãããããå éšã©ã€ãã©ãªã®å€éšã³ãŒããžã®äŸåãæžãããããã·ã¹ãã ãããæè»ã«éçºã§ããŸãã
- é©åã«èšèšãããAPIã®è²§åŒ±ã«èšèšãããã³ã¬ã¯ã·ã§ã³ãã©ããããŸãïŒã¿ã¹ã¯ã®ããŒãºã«å¿ããŠïŒ
AngularJSã«ã¯ããã€ãã®ãã¡ãµãŒãããããŸãã äžéšã®æ©èœã«é«ã¬ãã«ã®APIãæäŸãããã³ã«ãå®éã«ãã¡ãµãŒããäœæããŸãã
ããšãã°ãXMLHttpRequest POSTãªã¯ãšã¹ããäœæããæ¹æ³ãèŠãŠã¿ãŸãããã
var http = new XMLHttpRequest(), url = '/example/new', params = encodeURIComponent(data); http.open("POST", url, true); http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close"); http.onreadystatechange = function () { if(http.readyState == 4 && http.status == 200) { alert(http.responseText); } } http.send(params);
AngularJS $ httpãµãŒãã¹ã䜿çšããŠããŒã¿ãéä¿¡ããå Žåã次ã®ããšãã§ããŸãã
$http({ method: 'POST', url: '/example/new', data: data }) .then(function (response) { alert(response); });
ãŸãã¯ïŒ
$http.post('/someUrl', data).then(function (response) { alert(response); });
2çªç®ã®ãªãã·ã§ã³ã¯ãHTTP POSTèŠæ±ãäœæããäºåæ§æããŒãžã§ã³ã§ãã
3çªç®ã®ãªãã·ã§ã³ã¯ã$ãªãœãŒã¹ãµãŒãã¹ã䜿çšããŠäœæããã$ httpãµãŒãã¹ã®äžã«æ§ç¯ãããé«ã¬ãã«ã®æœè±¡åã§ãã Active Recordããã³Proxyã»ã¯ã·ã§ã³ã§ããã®ãµãŒãã¹ãããäžåºŠèŠãŠãããŸãã
ãããã·
ãããã·ïŒå¯ïŒ-æ§é èšèšãã³ãã¬ãŒãã¯ãå¥ã®ãªããžã§ã¯ããžã®ã¢ã¯ã»ã¹ãå¶åŸ¡ãããªããžã§ã¯ããæäŸãããã¹ãŠã®åŒã³åºããã€ã³ã¿ãŒã»ããããŸãïŒã³ã³ãããŒã®æ©èœãå®è¡ããŸãïŒã ãããã·ã¯ããããã¯ãŒã¯æ¥ç¶ãã¡ã¢ãªå ã®å€§ããªãªããžã§ã¯ãããã¡ã€ã«ããŸãã¯ã³ããŒãé«äŸ¡ãŸãã¯äžå¯èœãªãã®ä»ã®ãªãœãŒã¹ãªã©ããããããã®ãšå¯Ÿè©±ã§ããŸãã
3çš®é¡ã®ãããã·ãåºå¥ã§ããŸãã
- ä»®æ³ãããã·
- ãªã¢ãŒããããã·
- ä¿è·ãããã·
ãã®ãµãã»ã¯ã·ã§ã³ã§ã¯ãå®è£ ãããAngularJSä»®æ³ãããã·ãèŠãŠãããŸãã
以äžã®ã¹ããããã§ã¯ãUserãšããååã®$ãªãœãŒã¹ãªããžã§ã¯ãã®getã¡ãœãããåŒã³åºããŸãã
var User = $resource('/users/:id'), user = User.get({ id: 42 }); console.log(user); //{}
console.logã«ã¯ç©ºã®ãªããžã§ã¯ãã衚瀺ãããŸãã AJAXãªã¯ãšã¹ãã¯User.getã®åŒã³åºãåŸã«éåæã«å®è¡ããããããconsole.logã®åŒã³åºãäžã«ã¯ãŠãŒã¶ãŒããŒã¿ããããŸããã User.getãåŒã³åºããçŽåŸã«ãGETèŠæ±ãå®è¡ããã空ã®ãªããžã§ã¯ããè¿ããããã®ãªããžã§ã¯ããžã®ãªã³ã¯ãä¿åãããŸãã ãã®ãªããžã§ã¯ãã¯ä»®æ³ãããã·ãšèããããšãã§ããŸããã¯ã©ã€ã¢ã³ãããµãŒããŒããå¿çãåä¿¡ãããšããã«ããŒã¿ã§æºããããŸãã
AngularJSã§ã¯ã©ã®ããã«æ©èœããŸããïŒ æ¬¡ã®ã¹ãããããèŠãŠã¿ãŸãããã
function MainCtrl($scope, $resource) { var User = $resource('/users/:id'), $scope.user = User.get({ id: 42 }); }
<span ng-bind="user.name"></span>
äžèšã®ã³ãŒããã©ã°ã¡ã³ããå®è¡ãããšã$ã¹ã³ãŒããªããžã§ã¯ãã®ãŠãŒã¶ãŒããããã£ã¯ç©ºã®ãªããžã§ã¯ãïŒ{}ïŒã«ãªããŸããã€ãŸããuser.nameã¯æªå®çŸ©ã§è¡šç€ºãããŸããã ãµãŒããŒãGETèŠæ±ã«å¯Ÿããå¿çãè¿ããåŸãAngularJSã¯ãã®ãªããžã§ã¯ãã«ãµãŒããŒããåä¿¡ããããŒã¿ãå ¥åããŸãã 次ã®$ãã€ãžã§ã¹ããµã€ã¯ã«ã§ãAngularJSã¯$ scope.userã®å€æŽãæ€åºãããã¥ãŒãæŽæ°ããŸãã
ã¢ã¯ãã£ããªèšé²
ã¢ã¯ãã£ãã¬ã³ãŒãã¯ãããŒã¿ãšåäœãä¿åãããªããžã§ã¯ãã§ãã éåžžããã®ãªããžã§ã¯ãã®ã»ãšãã©ã®ããŒã¿ã¯æ°žç¶çã§ããããŒã¿ãäœæãæŽæ°ãæ€çŽ¢ãåé€ããããã«ããŒã¿ããŒã¹ã«æ¥ç¶ããã®ã¯Active Recordãªããžã§ã¯ãã®è²¬ä»»ã§ãã 圌ã¯ãã®è²¬ä»»ãäžäœã¬ãã«ã®ãªããžã§ã¯ãã«å§ä»»ã§ããŸãããActive Recordãªããžã§ã¯ããŸãã¯ãã®éçã¡ãœãããžã®åŒã³åºãã¯ãããŒã¿ããŒã¹ãšã®å¯Ÿè©±ã«ã€ãªãããŸãã
AngularJSã¯service $ãªãœãŒã¹ãå®çŸ©ããŸãã çŸåšã®ããŒãžã§ã³ã®AngularJSïŒ1,2+ïŒã§ã¯ãåå¥ã®ã¢ãžã¥ãŒã«ãšããŠé åžãããŠãããã«ãŒãã«ã«ã¯å«ãŸããŠããŸããã
$ãªãœãŒã¹ããã¥ã¡ã³ãã«ãããšïŒ
$ãªãœãŒã¹ã¯ãRESTfulãµãŒããŒåŽããŒã¿ãœãŒã¹ãšããåãã§ããããã«ãã$ãªãœãŒã¹ãªããžã§ã¯ããäœæããããã®ãã¡ã¯ããªã§ãã $ãªãœãŒã¹ãªããžã§ã¯ãã«ã¯ãäœã¬ãã«ã®ãµãŒãã¹ 'ohm $ httpãšå¯Ÿè©±ããå¿ èŠãªããé«ã¬ãã«ã®åäœãæäŸããã¡ãœããããããŸãã
$ãªãœãŒã¹ã®äœ¿çšæ¹æ³ã¯æ¬¡ã®ãšããã§ãã
var User = $resource('/users/:id'), user = new User({ name: 'foo', age : 42 }); user.$save();
$ãªãœãŒã¹ãåŒã³åºããšãã¢ãã«ã®ã€ã³ã¹ã¿ã³ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãäœæãããŸãã åã¢ãã«ã€ã³ã¹ã¿ã³ã¹ã«ã¯ãããŸããŸãªCRUDæäœã«äœ¿çšã§ããã¡ãœããããããŸãã
ãããã£ãŠãã³ã³ã¹ãã©ã¯ã¿ãŒé¢æ°ãšãã®éçã¡ãœããã䜿çšã§ããŸãã
User.get({ userid: userid });
äžèšã®ã³ãŒãã¯ããã«ç©ºã®ãªããžã§ã¯ããè¿ãããããžã®ãªã³ã¯ãä¿åããŸãã åçãåä¿¡ããŠââåæããåŸãAngularJSã¯ãªããžã§ã¯ãã«åä¿¡ããŒã¿ãå ¥åããŸãïŒãããã·ãåç §ïŒã
$ãªãœãŒã¹ãšAngularJSãªããžã§ã¯ãã®éæ³ã«é¢ãã詳现ãªããã¥ã¡ã³ããèŠã€ããããšãã§ããŸãã
ããŒãã£ã³ã»ãã¡ãŠã©ãŒã¯ãã䞻匵ããŠããŸãïŒ
Active Recordãªããžã§ã¯ãã¯ãããŒã¿ããŒã¹ãšã®éä¿¡ãåŠçããŠ...
$ãªãœãŒã¹ã¯ãããŒã¿ããŒã¹ã§ã¯ãªãRESTfulãµãŒãã¹ãšå¯Ÿè©±ãããããå®å šãªActive Recordãã³ãã¬ãŒããå®è£ ããŸããã ãããã«ããŠãããRESTfulãšå¯Ÿè©±ããããã®ã¢ã¯ãã£ãã¬ã³ãŒãããšèŠãªãããšãã§ããŸãã
ã€ã³ã¿ãŒã»ãããã£ã«ã¿ãŒ
ãã£ã«ã¿ãŒã®ãã§ãŒã³ãäœæããŠãåçŽãªããª/ãã¹ããªã¯ãšã¹ãåŠçã¿ã¹ã¯ãå®è¡ããŸãã
å Žåã«ãã£ãŠã¯ãHTTPãªã¯ãšã¹ãã®äœããã®çš®é¡ã®ååŠç/åŸåŠçãè¡ãå¿ èŠããããŸãã ãã®å Žåãã€ã³ã¿ãŒã»ãããã£ã«ã¿ãŒã¯ãHTTPèŠæ±/å¿çã®å/åŸåŠçããã®ã³ã°ãã»ãã¥ãªãã£ããŸãã¯èŠæ±æ¬æãŸãã¯ããããŒãå¿ èŠãšãããã®ä»ã®ã¿ã¹ã¯çšã«èšèšãããŠããŸãã éåžžãIntercepting Filtersãã³ãã¬ãŒãã«ã¯äžé£ã®ãã£ã«ã¿ãŒãå«ãŸãããããããç¹å®ã®é åºã§ããŒã¿ãåŠçããŸãã åãã£ã«ã¿ãŒã®åºåã¯ã次ã®ãã£ã«ã¿ãŒã®å ¥åã§ãã
AngularJSã«ã¯ã$ httpProviderã®ã€ã³ã¿ãŒã»ãããã£ã«ã¿ãŒã®ã¢ã€ãã¢ããããŸãã $ httpProviderã«ã¯interceptorsããããã£ãããããªããžã§ã¯ãã®ãªã¹ããå«ãŸããŠããŸãã åãªããžã§ã¯ãã«ã¯ãrequestãresponseãrequestErrorãresponseErrorã®ããããã£ããããŸãã
requestErrorã¯ã以åã®ã€ã³ã¿ãŒã»ãã¿ãŒã§ãšã©ãŒãçºçããå ŽåããŸãã¯æåŠãããå Žåã«ããããçºçããŸãã以åã®å¿çã€ã³ã¿ãŒã»ãã¿ãŒãäŸå€ãã¹ããŒããå ŽåãresponseErrorãçºçããŸãã
以äžã¯ããªããžã§ã¯ããªãã©ã«ã䜿çšããŠã€ã³ã¿ãŒã»ãã¿ãŒãè¿œå ããæ¹æ³ã®åºæ¬çãªäŸã§ãã
$httpProvider.interceptors.push(function($q, dependency1, dependency2) { return { 'request': function(config) { // same as above }, 'response': function(response) { // same as above } }; });
æ什
ã³ã³ããžãã
è€åãã³ãã¬ãŒãã¯ãæ§é èšèšãã¿ãŒã³ã§ããè€åãã³ãã¬ãŒãã¯ãåäžã®ãªããžã§ã¯ãã ãã§ãªããã¢ã¯ã»ã¹ã§ããããã«ãªããžã§ã¯ããã°ã«ãŒãåããæ¹æ³ã説æããŸããã³ã³ããžããã®ç®çã¯ãç¹å®ããå šäœãžã®éå±€ãè¡šãããªãŒæ§é ã§ãªããžã§ã¯ããæ§æããããšã§ãã
The Gang of Fourã«ãããšãMVCã¯çµã¿åããã«ãããŸããã
- æŠç¥
- ã³ã³ããžãã
- ãªãã¶ãŒããŒ
圌ãã¯ãããã©ãŒãã³ã¹ã¯ã³ã³ããŒãã³ãã®åæã§ãããšäž»åŒµããŠããŸããAngularJSã«ãåæ§ã®ç¶æ³ããããŸãããã¥ãŒã¯ããã£ã¬ã¯ãã£ããšãããã®ãã£ã¬ã¯ãã£ããæ§ç¯ãããDOMèŠçŽ ã®æ§æã«ãã£ãŠåœ¢æãããŸãã
次ã®äŸãèŠãŠã¿ãŸãããã
<!doctype html> <html> <head> </head> <body> <zippy title="Zippy"> Zippy! </zippy> </body> </html>
myModule.directive('zippy', function () { return { restrict: 'E', template: '<div><div class="header"></div><div class="content" ng-transclude></div></div>', link: function (scope, el) { el.find('.header').click(function () { el.find('.content').toggle(); }); } } });
ãã®äŸã§ã¯ããŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ã®ã³ã³ããŒãã³ãã§ãããã£ã¬ã¯ãã£ããäœæããŸããäœæãããã³ã³ããŒãã³ãïŒãzippyããšããååïŒã«ã¯ãã¿ã€ãã«ãšã³ã³ãã³ãããããŸããã¿ã€ãã«ãã¯ãªãã¯ãããšãã³ã³ãã³ãã®è¡šç€ºãåãæ¿ãããŸãã
æåã®äŸãããDOMããªãŒã¯èŠçŽ ã®çµã¿åããã§ããããšãããããŸããã«ãŒãã³ã³ããŒãã³ãã¯htmlã§ããã®çŽåŸã«ãã¹ããããèŠçŽ headãbodyãªã©ããããŸã...
2çªç®ã®äŸã§ã¯ããã³ãã¬ãŒããã£ã¬ã¯ãã£ãã®ããããã£ã«ng-transcludeãã£ã¬ã¯ãã£ããå«ãããŒã¯ã¢ãããå«ãŸããŠããããšãããããŸããããã¯ããzippyããã£ã¬ã¯ãã£ãå ã«å¥ã®ng-transcludeãã£ã¬ã¯ãã£ããã€ãŸããã£ã¬ã¯ãã£ãã®æ§æãããããšãæå³ããŸããçè«çã«ã¯ãæçµããŒãã«å°éãããŸã§ã³ã³ããŒãã³ããç¡éã«ãã¹ãã§ããŸãã
éèš³
ã€ã³ã¿ãŒããªã¿ãŒïŒã€ã³ã¿ãŒããªã¿ãŒïŒ-èšèªã§åŒãå®çŸ©ããæ¹æ³ã瀺ãåäœèšèšãã¿ãŒã³ãäž»ãªã¢ã€ãã¢ã¯ãåæåïŒç«¯æ«ãŸãã¯é端æ«ïŒãç¹æ®ãªããã°ã©ãã³ã°èšèªã«åé¡ããããšã§ããåŒã®æ§æããªãŒïŒæ§æãã³ãã¬ãŒãã®äŸïŒã¯ãåŒã®åæïŒè§£éïŒã«äœ¿çšãããŸãã
$ parseã䜿çšããŠãAngularJSã¯DSLïŒãã¡ã€ã³åºæèšèªïŒã€ã³ã¿ãŒããªã¿ãŒã®ç¬èªã®å®è£ ãæäŸããŸããDSLã䜿çšãããšãJavaScriptãç°¡çŽ åããã³å€æŽãããŸããAngularJSãšJavaScriptã®åŒã®äž»ãªéãã¯ãAngularJSã®åŒïŒ
- UNIXã©ã€ã¯ãªæ§æã®ãã£ã«ã¿ãŒãå«ããããšãã§ããŸã
- äŸå€ãçºçãããªã
- ç¶æ ãããŒãå¶åŸ¡ã§ããŸããïŒäŸå€ãã«ãŒããæ¡ä»¶ã«ãäžé æŒç®åã䜿çšã§ããŸãïŒ
- çµæã®ã³ã³ããã¹ãïŒçŸåšã®$ã¹ã³ãŒãã®ã³ã³ããã¹ãïŒã§å®è¡ããŸã
$ parseå ã§ã¯ã2ã€ã®äž»èŠã³ã³ããŒãã³ããå®çŸ©ãããŠããŸãã
// var Lexer; // var Parser;
åŒãåä¿¡ããããšãããŒã¯ã³ã«åå²ãããŠãã£ãã·ã¥ãããŸãïŒããã©ãŒãã³ã¹ã®åé¡ã®ããïŒã
AngularJS DSLã®ç«¯æ«åŒã¯ã次ã®ããã«å®çŸ©ãããŠããŸãã
var OPERATORS = { /* jshint bitwise : false */ 'null':function(){return null;}, 'true':function(){return true;}, 'false':function(){return false;}, undefined:noop, '+':function(self, locals, a,b){ //... }, '*':function(self, locals, a,b){return a(self, locals)*b(self, locals);}, '/':function(self, locals, a,b){return a(self, locals)/b(self, locals);}, '%':function(self, locals, a,b){return a(self, locals)%b(self, locals);}, '^':function(self, locals, a,b){return a(self, locals)^b(self, locals);}, '=':noop, '===':function(self, locals, a, b){return a(self, locals)===b(self, locals);}, '!==':function(self, locals, a, b){return a(self, locals)!==b(self, locals);}, '==':function(self, locals, a,b){return a(self, locals)==b(self, locals);}, '!=':function(self, locals, a,b){return a(self, locals)!=b(self, locals);}, '<':function(self, locals, a,b){return a(self, locals)<b(self, locals);}, '>':function(self, locals, a,b){return a(self, locals)>b(self, locals);}, '<=':function(self, locals, a,b){return a(self, locals)<=b(self, locals);}, '>=':function(self, locals, a,b){return a(self, locals)>=b(self, locals);}, '&&':function(self, locals, a,b){return a(self, locals)&&b(self, locals);}, '||':function(self, locals, a,b){return a(self, locals)||b(self, locals);}, '&':function(self, locals, a,b){return a(self, locals)&b(self, locals);}, '|':function(self, locals, a,b){return b(self, locals)(self, locals, a(self, locals));}, '!':function(self, locals, a){return !a(self, locals);} };
åçµç«¯èšå·ã«é¢é£ä»ããããåé¢æ°ã¯ãAbstractExpressionã€ã³ã¿ãŒãã§ã€ã¹ïŒæœè±¡åŒïŒã®å®è£ ãšããŠè¡šãããšãã§ããŸãã
ã¯ã©ã€ã¢ã³ãã¯ãç¹å®ã®ã³ã³ããã¹ãïŒç¹å®ã®$ã¹ã³ãŒãïŒã§åä¿¡ããåŒã解éããŸãã
ããã€ãã®ç°¡åãªAngularJSåŒïŒ
// toUpperCase filter is applied to the result of the expression // (foo) ? bar : baz (foo) ? bar : baz | toUpperCase
ãã³ãã¬ãŒããã¥ãŒ
HTMLããŒãžã«ããŒã«ãŒãåã蟌ãããšã«ãããããŒã¿ãHTMLã«å€æããŸãã
ããŒãžãåçã«è¡šç€ºããããšã¯ãããã»ã©ç°¡åãªäœæ¥ã§ã¯ãããŸãããããã¯ãå€ãã®æååã®é£çµãæäœãããã³åé¡ãåå ã§ããåçããŒãžãäœæããæãç°¡åãªæ¹æ³ã¯ãããŒã¯ã¢ãããèšè¿°ããç¹å®ã®ã³ã³ããã¹ãã§åŠçãããããã€ãã®åŒããã®äžã«å«ããããšã§ãããã®çµæããã³ãã¬ãŒãå šäœãæçµçãªåœ¢åŒã«å€æãããŸããç§ãã¡ã®å Žåããã®åœ¢åŒã¯HTMLïŒãŸãã¯DOMïŒã«ãªããŸããããããŸãã«ãã³ãã¬ãŒããšã³ãžã³ã®åäœã§ããDSLãååŸããé©åãªã³ã³ããã¹ãã§åŠçããŠãããæçµçãªåœ¢åŒã«å€æããŸãã
ãã³ãã¬ãŒãã¯ããã¯ãšã³ãã§éåžžã«é »ç¹ã«äœ¿çšãããŸããããšãã°ãHTMLã«PHPã³ãŒããåã蟌ã¿ãåçããŒãžãäœæã§ããŸãããŸããRubyã§SmartyãŸãã¯eRubyã䜿çšããŠãéçããŒãžã«Rubyã³ãŒããåã蟌ãããšãã§ããŸãã
JavaScriptã«ã¯ãmustache.jsããã³ãã«ããŒãªã©ã®å€ãã®ãã³ãã¬ãŒããšã³ãžã³ããããŸãããããã®ã»ãšãã©ã¯ããã³ãã¬ãŒããæååãšããŠäœ¿çšããŸãããã³ãã¬ãŒãã¯ããŸããŸãªæ¹æ³ã§ä¿åã§ããŸãã
- AJAXãåä¿¡ããéçãã¡ã€ã«
- ãã¥ãŒå ã«åã蟌ãŸããã¹ã¯ãªãã
- JavaScriptã³ãŒãå ã®è¡
äŸïŒ
<script type="template/mustache"> <h2>Names</h2> {{#names}} <strong>{{name}}</strong> {{/names}} </script>
ãã³ãã¬ãŒããšã³ãžã³ã¯ãæååãçµæã®ã³ã³ããã¹ããšçµã¿åãããŠãDOMèŠçŽ ã«å€æããŸãããããã£ãŠãããŒã¯ã¢ããã«çµã¿èŸŒãŸããŠãããã¹ãŠã®åŒãåæããããããã®å€ã«çœ®ãæããããŸãã
ããšãã°ããªããžã§ã¯ãã®ã³ã³ããã¹ãã§äžèšã®ãã³ãã¬ãŒããåŠçãããšã{namesïŒ['foo'ã 'bar'ã 'baz']}ãåŸãããŸãã
<h2>Names</h2> <strong>foo</strong> <strong>bar</strong> <strong>baz</strong>
å®éãAngularJSã®ãã³ãã¬ãŒãã¯ãã¬ãŒã³HTMLã§ããã»ãšãã©ã®ãã³ãã¬ãŒãã®ããã«ããããã¯äžé圢åŒã§ã¯ãããŸãããAngularJSã³ã³ãã€ã©ã¯ãDOMããªãŒããã€ãã¹ããŠæ¢ç¥ã®ãã£ã¬ã¯ãã£ãïŒèŠçŽ ãå±æ§ãã¯ã©ã¹ããŸãã¯ã³ã¡ã³ãïŒãèŠã€ããããã«äœãããŸããïŒAngularJSã¯ããããã®ãã£ã¬ã¯ãã£ãã®ãããããæ€åºãããšãããã«é¢é£ä»ããããããžãã¯ãåŒã³åºããçŸåšã®$ã¹ã³ãŒãã®ã³ã³ããã¹ãã«ããŸããŸãªåŒã®å®çŸ©ãå«ããããšãã§ããŸãã
äŸïŒ
<ul ng-repeat="name in names"> <li>{{name}}</li> </ul>
ã¹ã³ãŒãã³ã³ããã¹ãã§ïŒ
$scope.names = ['foo', 'bar', 'baz'];
äžèšãšåãçµæãåŸãããŸããäž»ãªéãã¯ããã³ãã¬ãŒããscriptã¿ã°å ã«ãªãããšã§ããããã§ã¯HTMLã®ã¿ã§ãã
ç¯å²
ãªãã¶ãŒããŒ
ãªãã¶ãŒããŒãã¿ãŒã³ã¯ããµããžã§ã¯ããšåŒã°ãããªããžã§ã¯ãããªãã¶ãŒããŒãšåŒã°ããäŸåé¢ä¿ã®ãªã¹ããä¿åããéåžžã¯ã¡ãœããã®1ã€ãåŒã³åºãããšã§ç¶æ ã«å€åããã£ãå Žåã«éç¥ããåäœèšèšãã¿ãŒã³ã§ããäž»ã«ãåæ£ã€ãã³ãåŠçã·ã¹ãã ã®å®è£ ã«äœ¿çšãããŸãã
AngularJSã¢ããªã±ãŒã·ã§ã³ã«ã¯ãã¹ã³ãŒãéã§çžäºäœçšãã2ã€ã®äž»ãªæ¹æ³ããããŸãã
ãŸããåã¹ã³ãŒããã芪ã¹ã³ãŒãã®ã¡ãœãããåŒã³åºããŸããåè¿°ã®ãšãããåã¹ã³ãŒãã¯èŠªã®ãããã¿ã€ããç¶æ¿ããããããããå¯èœã§ãïŒã¹ã³ãŒããåç §ïŒãããã«ãããåãã芪ãžã®äžæ¹åã®å¯Ÿè©±ãå¯èœã«ãªããŸããåã¹ã³ãŒãã®ã¡ãœãããåŒã³åºããã芪ã¹ã³ãŒãã§ã®ã€ãã³ãã®çºçãéç¥ããå¿ èŠãããå ŽåããããŸãã AngularJSã¯ããããå¯èœã«ããçµã¿èŸŒã¿ã®ãªãã¶ãŒããŒãã¿ãŒã³ãæäŸããŸãã
ãªãã¶ãŒããŒãã¿ãŒã³ã®2çªç®ã®å¯èœãªäœ¿çšæ³ã¯ãè€æ°ã®ã¹ã³ãŒããã€ãã³ãã«ãµãã¹ã¯ã©ã€ããããå Žåã§ããããããçºçããã¹ã³ãŒãã¯ããã«ã€ããŠäœãç¥ããŸãããããã«ãããæ¥ç¶ã®ç¯å²ãçž®å°ã§ããŸããä»ã®ç¯å²ã«ã€ããŠã¯äœãç¥ããªãããã«ããå¿ èŠããããŸãã
AngularJSã®åã¹ã³ãŒãã«ã¯ããããªãã¯ã¡ãœãã$ onã$ emitãããã³$ broadcastããããŸãã$ onã¡ãœããã¯ãã€ãã³ãåãšã³ãŒã«ããã¯é¢æ°ãåŒæ°ãšããŠåãåããŸãããã®é¢æ°ã¯ããªãã¶ãŒããŒïŒãªãã¶ãŒããŒã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ãããªããžã§ã¯ãïŒãšããŠè¡šãããšãã§ããŸãïŒJavaScriptã§ã¯ããã¹ãŠã®é¢æ°ã¯æåã®ã¯ã©ã¹ã§ãããããéç¥ã¡ãœããã®å®è£ ã®ã¿ãæäŸã§ããŸãïŒã
function ExampleCtrl($scope) { $scope.$on('event-name', function handler() { //body }); }
ãããã£ãŠãçŸåšã®ã¹ã³ãŒãã¯event-nameã€ãã³ãã«ãµãã¹ã¯ã©ã€ãããŸãã芪ã¹ã³ãŒããŸãã¯åã¹ã³ãŒãã®ããããã§çºçãããšããã³ãã©ãŒãåŒã³åºãããŸãã
$ emitããã³$ broadcastã¡ãœããã¯ãããããç¶æ¿ãã§ãŒã³ã®äžäžã§ã€ãã³ããããªã¬ãŒããããã«äœ¿çšãããŸããäŸïŒ
function ExampleCtrl($scope) { $scope.$emit('event-name', { foo: 'bar' }); }
äžèšã®äŸã§ã¯ãã¹ã³ãŒãã¯ãã¹ãŠã®ã¹ã³ãŒãã«å¯ŸããŠã€ãã³ãåã€ãã³ããçæããŸããããã¯ãevent-nameã€ãã³ãã«ãµãã¹ã¯ã©ã€ããããã¹ãŠã®èŠªã¹ã³ãŒããéç¥ããããã®ãã³ãã©ãŒãåŒã³åºãããããšãæå³ããŸãã
$ broadcastã¡ãœãããåŒã³åºããããšãã«ãåãããšãèµ·ãããŸããå¯äžã®éãã¯ãã€ãã³ãããã¹ãŠã®åã¹ã³ãŒãã«å¯ŸããŠæž¡ãããããšã§ããåã¹ã³ãŒãã¯ãè€æ°ã®ã³ãŒã«ããã¯é¢æ°ã䜿çšããŠä»»æã®ã€ãã³ãããµãã¹ã¯ã©ã€ãã§ããŸãïŒã€ãŸããè€æ°ã®ãªãã¶ãŒããŒããã®ã€ãã³ãã«é¢é£ä»ããããšãã§ããŸãïŒã
JavaScriptã³ãã¥ããã£ã§ã¯ããã®ãã³ãã¬ãŒãã¯ãããªãã·ã¥/ãµãã¹ã¯ã©ã€ããšããŠç¥ãããŠããŸãã
責任ã®é£é
Chain of ResponsibilitiesïŒè¡åé£éïŒ-è¡åãã¶ã€ã³ãã¿ãŒã³ã¯ãããŒã ãªããžã§ã¯ããšäžé£ã®åŠçãªããžã§ã¯ãïŒãã³ãã©ãŒïŒã§æ§æãããŸããåãã³ãã©ãŒã«ã¯ãåŠçã§ããã³ãã³ãã®ã¿ã€ãã決å®ããããžãã¯ãå«ãŸããŠããŸãã次ã«ãã³ãã³ãã¯ãã§ãŒã³å ã®æ¬¡ã®ãã³ãã©ãŒã«ç§»åããŸãããã³ãã¬ãŒãã«ã¯ããã®ãã§ãŒã³ã®æåŸã«æ°ãããã³ãã©ãŒãè¿œå ããã¡ã«ããºã ãå«ãŸããŠããŸãã
äžèšã®ããã«ãã¹ã³ãŒãã¯ã¹ã³ãŒããã§ãŒã³ãšåŒã°ããéå±€ã圢æããŸãããããã®ã¹ã³ãŒãã®äžéšã¯åé¢ãããŠããŸããã€ãŸãã芪ã¹ã³ãŒãã®ãããã¿ã€ããç¶æ¿ããŸãããã$ parentããããã£ãä»ããŠãããã¿ã€ãã«é¢é£ä»ããããŸãã
$ emitãŸãã¯$ broadcastãåŒã³åºããåŸãã¹ã³ãŒããã§ãŒã³ã«æ²¿ã£ãŠïŒåŒã³åºãããã¡ãœããã«å¿ããŠäžäžã«ïŒç§»åãéå§ããã€ãã³ããçºçããã€ãã³ããã¹ãšããŠãããæ£ç¢ºã«ã¯è·åã®ãã§ãŒã³ãšããŠè¡šãããšãã§ããŸããåŸç¶ã®åã¹ã³ãŒãã¯æ¬¡ã®ããšãã§ããŸãã
- ã€ãã³ããåŠçãããã§ãŒã³å ã®æ¬¡ã®ã¹ã³ãŒãã«æž¡ããŸã
- ã€ãã³ããåŠçãããã®é ä¿¡ãåæ¢ããŸã
- ã€ãã³ããåŠçããã«ãã§ãŒã³å ã®æ¬¡ã®ã¹ã³ãŒãã«ã¹ãããããŸã
- åŠçããã«ã€ãã³ãã®äŒæãåæ¢ãã
以äžã®äŸã§ã¯ãChildCtrlã¯ã¹ã³ãŒããã§ãŒã³ãäžã«äŒæããã€ãã³ããçæããŸããããã§ãå芪ã¹ã³ãŒãïŒParentCtrlããã³MainCtrlïŒã¯ãã³ã³ãœãŒã«ã«æžã蟌ãããšã§ã€ãã³ããåŠçããå¿ èŠããããŸãïŒãfoo receivedããã¹ã³ãŒãã®ãããããæçµåä¿¡è ã§ããå Žåãã€ãã³ããªããžã§ã¯ãã§stopPropagationã¡ãœãããåŒã³åºãããšãã§ããŸãã
myModule.controller('MainCtrl', function ($scope) { $scope.$on('foo', function () { console.log('foo received'); }); }); myModule.controller('ParentCtrl', function ($scope) { $scope.$on('foo', function (e) { console.log('foo received'); }); }); myModule.controller('ChildCtrl', function ($scope) { $scope.$emit('foo'); });
äžèšã®UMLå³ã®ãã³ãã©ãŒã¯ãã³ã³ãããŒã©ãŒã«è¿œå ãããããŸããŸãªã¹ã³ãŒãã§ãã
ã³ãã³ã
ã³ãã³ãã¯ããªããžã§ã¯ãã䜿çšããŠå¿ èŠãªãã¹ãŠã®æ å ±ãã«ãã»ã«åãããã°ããããŠããã¡ãœãããåŒã³åºãããã€ãã¢ãŒãã¶ã€ã³ãã¿ãŒã³ã§ãããã®æ å ±ã«ã¯ãã¡ãœããã®ååãã¡ãœãããå±ãããªããžã§ã¯ããããã³ã¡ãœãããã©ã¡ãŒã¿ãŒã®å€ãå«ãŸããŸãã
AngularJSã§ã¯ãã³ãã³ããã¿ãŒã³ã䜿çšããŠãããŒã¿ãã€ã³ãã£ã³ã°ã®å®è£ ãèšè¿°ã§ããŸãã
ã¢ãã«ããã¥ãŒã«ãªã³ã¯ããå Žåãng-bindãã£ã¬ã¯ãã£ãïŒäžæ¹åããŒã¿ãã€ã³ãã£ã³ã°çšïŒããã³ng-modelïŒåæ¹åããŒã¿ãã€ã³ãã£ã³ã°çšïŒã䜿çšã§ããŸããããšãã°ãã¢ãã«ã®ãã¹ãŠã®å€æŽããã¥ãŒã«è¡šç€ºããå ŽåïŒ
<span ng-bind="foo"></span>
fooã®å€ãå€æŽãããã³ã«ãspanã¿ã°å ã®ããã¹ããå€æŽãããŸããããè€éãªåŒã䜿çšããããšãã§ããŸãã
<span ng-bind="foo + ' ' + bar | uppercase"></span>
äžèšã®äŸã§ã¯ãspanã¿ã°ã®å€ã¯å€§æåã®fooãšbarã®å€ã®åèšã«ãªããŸããå éšã§äœãèµ·ãã£ãŠããã®ã§ããïŒ
åã¹ã³ãŒãã«ã¯$ watchã¡ãœããããããŸããAngularJSã³ã³ãã€ã©ãng-bindãã£ã¬ã¯ãã£ããèŠã€ãããšãåŒfoo + '' + bar |ã®æ°ãããªãã¶ãŒããŒãäœæããŸãã倧æåãïŒ$ã¹ã³ãŒãã$ watchïŒ "foo + '' + bar | uppercase"ãfunctionïŒïŒ{/ * body * /}ïŒ;ïŒãã³ãŒã«ããã¯é¢æ°ã¯ãåŒã®å€ãå€æŽããããã³ã«åŒã³åºãããŸãããã®å Žåãã³ãŒã«ããã¯é¢æ°ã¯spanã¿ã°ã®å€ãæŽæ°ããŸãã
$ watchå®è£ ã®æåã®æ°è¡ã¯æ¬¡ã®ãšããã§ãã
$watch: function(watchExp, listener, objectEquality) { var scope = this, get = compileToFn(watchExp, 'watch'), array = scope.$$watchers, watcher = { fn: listener, last: initWatchVal, get: get, exp: watchExp, eq: !!objectEquality }; //...
ãŠã©ããã£ãŒã¯ã³ãã³ããšããŠæ³åã§ããŸããã³ãã³ãåŒã¯ã$ãã€ãžã§ã¹ãã«ãŒãããšã«è©äŸ¡ãããŸããAngularJSã¯åŒã®å€æŽãæ€åºãããšããªã¹ããŒé¢æ°ãåŒã³åºããŸããWatcherã«ã¯ãã³ãã³ãã®å®è¡ãç£èŠããŠãªã¹ããŒïŒå®éã®åä¿¡è ïŒã«å§ä»»ããããã«å¿ èŠãªãã¹ãŠã®æ å ±ãå«ãŸããŠããŸãã$ã¹ã³ãŒããClientãšããŠã$ãã€ãžã§ã¹ããµã€ã¯ã«ãInvokerããŒã ãšããŠè¡šãããšãã§ããŸãã
ã³ã³ãããŒã©ãŒ
ããŒãžã³ã³ãããŒã©ãŒ
ãµã€ãäžã®ç¹å®ã®ããŒãžãŸãã¯ã¢ã¯ãã£ããã£ã«å¯ŸããèŠæ±ãåŠçãããªããžã§ã¯ããããŒãã£ã³ã»ãã¡ãŠã©ãŒã
ããŒãžããã4ã€ã®ã³ã³ãããŒã©ãŒã«ãããšïŒ
ããŒãžã³ã³ãããŒã©ãŒãã³ãã¬ãŒãã¯ãåä¿¡ããããŒãžããã®ããŒã¿å ¥åãèš±å¯ããã¢ãã«ã«å¯ŸããŠèŠæ±ãããã¢ã¯ã·ã§ã³ãå®è¡ããçµæã®ããŒãžã®æ£ãã衚瀺ã決å®ããŸãããã¥ãŒã³ãŒãã®æ®ãã®éšåããã®ãã£ã¹ãããããžãã¯ã®åé¢ã
ããŒãžäžã«å€§éã®ã³ãŒããéè€ããŠããããïŒããšãã°ãããã¿ãŒãããããŒããŠãŒã¶ãŒã®ã»ãã·ã§ã³ãåŠçããã³ãŒãïŒãã³ã³ãããŒã©ãŒã¯éå±€ã圢æã§ããŸãã AngularJSã«ã¯ãã¹ã³ãŒããå¶éãããã³ã³ãããŒã©ãŒããããŸãããããã¯ã$ routeãŸãã¯$ stateã®è²¬ä»»ã§ããããã£ã¬ã¯ãã£ãng-view / ui-viewã衚瀺ãæ åœããããããŠãŒã¶ãŒãªã¯ãšã¹ããåãå ¥ããŸããã
ããŒãžã³ã³ãããŒã©ãŒãšåæ§ã«ãAngularJSã³ã³ãããŒã©ãŒã¯ãŠãŒã¶ãŒãšã®ããåããæ åœããã¢ãã«ã®æŽæ°ãæäŸããŸãããããã®ã¢ãã«ã¯ãã¹ã³ãŒãã«è¿œå ãããåŸã衚瀺ããä¿è·ãããŸãã;ãã¥ãŒã«å«ãŸãããã¹ãŠã®ã¡ãœããã¯ãæçµçã«ãŠãŒã¶ãŒã¢ã¯ã·ã§ã³ïŒã¹ã³ãŒãã¡ãœããïŒã«ãªããŸããããŒãžã³ã³ãããŒã©ãŒãšAngularJSã³ã³ãããŒã©ãŒã®ãã1ã€ã®é¡äŒŒç¹ã¯ããããã圢æããéå±€ã§ããã¹ã³ãŒãéå±€ã¯ããã«å¯Ÿå¿ããŸãããããã£ãŠãç°¡åãªã¢ã¯ã·ã§ã³ã§ããŒã¹ã³ã³ãããŒã©ãŒãåé¢ã§ããŸãã
AngularJSã³ã³ãããŒã©ãŒã¯ãASP.NET WebFormsãšéåžžã«ãã䌌ãŠããŸãããã®åœ¹å²ã¯ã»ãŒåãã§ããè€æ°ã®ã³ã³ãããŒã©ãŒéã®éå±€ã®äŸã次ã«ç€ºããŸãã
<!doctype html> <html> <head> </head> <body ng-controller="MainCtrl"> <div ng-controller="ChildCtrl"> <span>{{user.name}}</span> <button ng-click="click()">Click</button> </div> </body> </html>
function MainCtrl($scope, $location, User) { if (!User.isAuthenticated()) { $location.path('/unauthenticated'); } } function ChildCtrl($scope, User) { $scope.click = function () { alert('You clicked me!'); }; $scope.user = User.get(0); }
ãã®äŸã¯ãããŒã¹ã³ã³ãããŒã©ãŒã䜿çšããŠããžãã¯ãåå©çšããæãç°¡åãªæ¹æ³ã瀺ããŠããŸãããå®çšŒåã¢ããªã±ãŒã·ã§ã³ã§ã¯ãã³ã³ãããŒã©ãŒã«æ¿èªããžãã¯ãé 眮ããããšã¯ãå§ãããŸãããããŸããŸãªã«ãŒããžã®ã¢ã¯ã»ã¹ã¯ãããé«ãæœè±¡ã¬ãã«ã§å®çŸ©ã§ããŸãã
ChildCtrã¯ãã¢ãã«ããã¥ãŒã«è¡šç€ºãããã¯ãªãã¯ããšãããã¿ã³ãã¯ãªãã¯ãããªã©ã®ã¢ã¯ã·ã§ã³ãåŠçããŸãã
ãã®ä»
ã¢ãžã¥ãŒã«ãã¿ãŒã³
å®éãããã¯ã4人çµãã®ãã¶ã€ã³ãã¿ãŒã³ã§ã¯ãªããããšã³ã¿ãŒãã©ã€ãºã¢ããªã±ãŒã·ã§ã³ã¢ãŒããã¯ãã£ã®ãã¿ãŒã³ãã®1ã€ã§ããããŸãããããã¯ãã«ãã»ã«åãæäŸããããšãç®çãšããåŸæ¥ã®JavaScriptãã¶ã€ã³ãã¿ãŒã³ã§ãã
ã¢ãžã¥ãŒã«ãã³ãã¬ãŒãã䜿çšãããšãæ©èœãšèªåœã®ç¯å²ã«åºã¥ããŠããŒã¿ãã©ã€ãã·ãŒãååŸã§ããŸããåã¢ãžã¥ãŒã«ã«ã¯ãããŒã«ã«ã¹ã³ãŒãã§é衚瀺ã«ãªã£ãŠãã0å以äžã®ãã©ã€ããŒãã¡ãœãããå«ããããšãã§ããŸãã
ãã®é¢æ°ã¯ããã®ã¢ãžã¥ãŒã«ã®ãããªãã¯APIãæäŸãããªããžã§ã¯ããè¿ããŸãã
var Page = (function () { var title; function setTitle(t) { document.title = t; title = t; } function getTitle() { return title; } return { setTitle: setTitle, getTitle: getTitle }; }());
äžèšã®äŸã§ã¯ãIIFEïŒImmediately-Invoked Function Expressionã¯ãããã«åŒã³åºãããé¢æ°ã®åŒã§ãïŒãåŒã³åºãåŸã2ã€ã®ã¡ãœããïŒsetTitleããã³getTitleïŒã§ãªããžã§ã¯ããè¿ããŸããè¿ããããªããžã§ã¯ãã¯ãPageå€æ°ã«å²ãåœãŠãããŸãããã®å ŽåãPageãªããžã§ã¯ãã®ãŠãŒã¶ãŒã¯ãIIFEã®ããŒã«ã«ã¹ã³ãŒãå ã§å®çŸ©ãããŠããå€æ°titleã«çŽæ¥ã¢ã¯ã»ã¹ã§ããŸããã
ã¢ãžã¥ãŒã«ãã³ãã¬ãŒãã¯ãAngularJSã§ãµãŒãã¹ãå®çŸ©ãããšãã«éåžžã«äŸ¿å©ã§ãããã®ãã³ãã¬ãŒãã䜿çšãããšããã©ã€ãã·ãŒãå®çŸã§ããŸãïŒå®éã«å®çŸã§ããŸãïŒã
app.factory('foo', function () { function privateMember() { //body... } function publicMember() { //body... privateMember(); //body } return { publicMember: publicMember }; });
fooãä»ã®ã³ã³ããŒãã³ãã«è¿œå ãããšããã©ã€ããŒãã¡ãœããã䜿çšã§ããªããªãããããªãã¯ã¡ãœããã®ã¿ã䜿çšã§ããããã«ãªããŸãããã®ãœãªã¥ãŒã·ã§ã³ã¯ãç¹ã«åå©çšã®ããã«ã©ã€ãã©ãªãäœæããå Žåã«éåžžã«åŒ·åã§ãã
ããŒã¿ããããŒ
ããŒã¿ããããŒãã³ãã¬ãŒãã¯ãæ°žç¶çãªããŒã¿ã¹ãã¢ïŒå€ãã®å Žåããªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ïŒãšã¡ã¢ãªå ããŒã¿ã®éã§åæ¹åã®ããŒã¿è»¢éãå®è¡ããããŒã¿ã¢ã¯ã»ã¹ã¬ã€ã€ãŒã§ãããã®ãã³ãã¬ãŒãã®ç®çã¯ãäºãã«ç¬ç«ããŠãæ°žç¶ããŒã¿ã®è¡šçŸãã¡ã¢ãªã«ä¿åããããšã§ãã
æ¢ã«è¿°ã¹ãããã«ãData Mapperã¯ãæ°žç¶ããŒã¿ã¹ãã¢ãšã¡ã¢ãªå ã®ããŒã¿éã®åæ¹åããŒã¿è»¢éã«äœ¿çšãããŸããéåžžãAngularJSã¢ããªã±ãŒã·ã§ã³ã¯ãä»»æã®ãµãŒããŒèšèªïŒRubyãPHPãJavaãJavaScriptãªã©ïŒã§èšè¿°ããããµãŒããŒAPIãšå¯Ÿè©±ããŸãã
RESTful APIãããå Žåãservice $ãªãœãŒã¹ã¯Active Recordã®ãããªã¹ã¿ã€ã«ã§ãµãŒããŒãšå¯Ÿè©±ããã®ã«åœ¹ç«ã¡ãŸããäžéšã®ã¢ããªã±ãŒã·ã§ã³ã¯ãããã³ããšã³ãã§äœ¿çšãããæé©ãªåœ¢åŒã§ãµãŒããŒããããŒã¿ãè¿ããŸãããã
ããšãã°ãåãŠãŒã¶ãŒã次ã®ãããªã¢ããªã±ãŒã·ã§ã³ãæã£ãŠãããšããŸãããïŒ
- ãåå
- äœæ
- åéã®ãªã¹ã
ãããŠã次ã®ã¡ãœãããæã€APIïŒ
- GET / user /ïŒid-åä¿¡ãããŠãŒã¶ãŒã®ååãšã¢ãã¬ã¹ãè¿ããŸã
- GET / friends /ïŒid-ãã®ãŠãŒã¶ãŒã®åéã®ãªã¹ããè¿ããŸã
1ã€ã®è§£æ±ºçã¯ã1ã€ç®ã®æ¹æ³ãš2ã€ç®ã®æ¹æ³ã®2ã€ã®ç°ãªããµãŒãã¹ã䜿çšããããšã§ãããããããããé©åãªè§£æ±ºçã¯ãUserãšãã1ã€ã®ãµãŒãã¹ã䜿çšããããšã§ããUserãèŠæ±ãããšããŠãŒã¶ãŒã®å人ãèªã¿èŸŒãŸããŸãã
app.factory('User', function ($q) { function User(name, address, friends) { this.name = name; this.address = address; this.friends = friends; } User.get = function (params) { var user = $http.get('/user/' + params.id), friends = $http.get('/friends/' + params.id); $q.all([user, friends]).then(function (user, friends) { return new User(user.name, user.address, friends); }); }; return User; });
ãããã£ãŠãSPAèŠä»¶ã«åŸã£ãŠAPIã«é©å¿ããæ¬äŒŒããŒã¿ããããŒãäœæããŸããã
Userã¯æ¬¡ã®ããã«äœ¿çšã§ããŸãã
function MainCtrl($scope, User) { User.get({ id: 1 }).then(function (data) { $scope.user = data; }); }
察å¿ãããã³ãã¬ãŒãïŒ
<div> <div> Name: {{user.name}} </div> <div> Address: {{user.address}} </div> <div> Friends with ids: <ul> <li ng-repeat="friend in user.friends">{{friend}}</li> </ul> </div> </div>
åç §è³æ
- ãŠã£ãããã£ã¢ãèšèšãã¿ãŒã³ã®ãã¹ãŠã®ç°¡åãªèª¬æã®ãœãŒã¹ã¯ãŠã£ãããã£ã¢ã§ãã
- AngularJS' documentation
- AngularJS' git repository
- Page Controller
- Patterns of Enterprise Application Architecture (P of EAA)
- Using Dependancy Injection to Avoid Singletons
- Why would one use the Publish/Subscribe pattern (in JS/jQuery)?