ã°ãã¼ãã«ã¤ã³ã¿ã¼ã»ãã¿ã¼
JavaScriptで動くDIコンテナã®è§£èª¬ãã®3ã
ã°ãã¼ãã«ã¤ã³ã¿ã¼ã»ãã¿ã¼ã¯ãã¹ã¦ã®ã³ã³ãã¼ãã³ãã«é©ç¨ãããã¤ã³ã¿ã¼ã»ãã¿ã¼ã§ããè¤æ°ã®ã³ã³ãã¼ãã³ãã«ä¸æ¬ãã¦ã¤ã³ã¿ã¼ã»ãã¿ãé©ç¨ãããå ´åã«ä½¿ãã¾ãã
- binder.bindInterceptor()ã§ã³ã³ããã«ç»é²ãã¾ããå¼æ°ã§ä»¥ä¸ãæå®ãã¾ãã
- ã¤ã³ã¿ã¼ã»ãã¿é¢æ°
- é©ç¨ããã³ã³ãã¼ãã³ãã示ãcontainer.Matcher
- ã³ã³ãã¼ãã³ãåãcontainer.Matcherã§è©ä¾¡ããtrueã¨ãªã£ããã®ã«ã¤ã³ã¿ã¼ã»ãã¿ãé©ç¨ããã¾ãã
- container.any()ã§ãã¹ã¦ã®ã³ã³ãã¼ãã³ãã«ãããããcontainer.Matcherãä½æã§ãã¾ãã
- é©ç¨ããã¡ã½ããã示ãcontainer.Matcher
- ã¡ã½ããåãcontainer.Matcherã§è©ä¾¡ããtrueã¨ãªã£ããã®ã«ã¤ã³ã¿ã¼ã»ãã¿ãé©ç¨ããã¾ãã
var stdout = document.getElementById( "stdout" ); // ã¯ã©ã¹ function Kitten( name, age ) { this.name = name; this.age = age; } Kitten.prototype = { getName: function () { return this.name; }, getAge: function () { return this.age; } }; // ã³ã³ãã var c = new container.Container( function( binder ) { binder.bind( "mii" ).to( Kitten ).inject( { "name": "mii", "age":1 }); binder.bind( "tora" ).to( Kitten ).inject( { "name": "tora", "age":2 }); // ã°ãã¼ãã«ã¤ã³ã¿ã¼ã»ãã¿ã¼ã®è¨å® binder.bindInterceptor( function ( mi ) { // ã¤ã³ã¿ã¼ã»ãã¿é¢æ° return mi.proceed() + "_global"; // çµæã®æ«å°¾ã«"_global"ãä»ãã¦è¿ãã }, container.any(), // é©ç¨ããã³ã³ãã¼ãã³ãã示ãcontainer.Matcher // ãã®å ´åãã¹ã¦ã®ã³ã³ãã¼ãã³ãã対象 container.any()); // é©ç¨ããã¡ã½ããã示ãcontainer.Matcher // ãã®å ´åãã¹ã¦ã®ã¡ã½ããã対象 }); var mii = c.get( "mii" ); stdout.innerHTML += mii.getName() + " : " + mii.getAge() + "<br/>"; // mii_global : 1_global var tora = c.get( "tora" ); stdout.innerHTML += tora.getName() + " : " + tora.getAge() + "<br/>"; // tora_global : 2_global
ã³ã³ãã¼ãã³ãã«åå¥ã«è¨å®ããã¤ã³ã¿ã¼ã»ãã¿ã¨ã®ä½µç¨
ã³ã³ãã¼ãã³ãã«åå¥ã«è¨å®ããã¤ã³ã¿ã¼ã»ãã¿ã¨ã®éããããå¯è½ã§ãããã®å ´åã以ä¸ã®é ã§ã¡ã½ãããå®è¡ããã¾ãã
- ã°ãã¼ãã«ã¤ã³ã¿ã¼ã»ãã¿
- ã³ã³ãã¼ãã³ãåå¥ã®ã¤ã³ã¿ã¼ã»ãã¿
- æ¬æ¥ã®ã¡ã½ãã
var c = new container.Container( function( binder ) { // ã³ã³ãã¼ãã³ãåå¥ã®ã¤ã³ã¿ã¼ã»ãã¿ã¨éããããå¯è½ã binder.bind( "tora" ).to( Kitten ).inject( { "name": "tora", "age":2 }); binder.bind( "mii" ).to( Kitten ).inject( { "name": "mii", "age":1 }) .intercept( function ( mi ) { // ã¤ã³ã¿ã¼ã»ãã¿ãè¨å® return mi.proceed() + "_i"; // çµæã®æ«å°¾ã«"_i"ãä»ãã¦è¿ãã }, container.any()); // ã°ãã¼ãã«ã¤ã³ã¿ã¼ã»ãã¿ã¼ã®è¨å® binder.bindInterceptor( function ( mi ) { return mi.proceed() + "_global"; }, new container.Matcher(/mii/), new container.Matcher(/getAge/)); }); var mii = c.get( "mii" ); stdout.innerHTML += mii.getName() + ":" + mii.getAge() + "<br/>"; // mii_i : 1_i_global var tora = c.get( "tora" ); stdout.innerHTML += tora.getName() + ":" + tora.getAge() + "<br/>"; // tora_ : 2