RequireJS moduleã«ã¤ãã¦
RequireJSã£ã¦ä½?
å
¬å¼ãµã¤ã
RequireJS
ã¹ã©ã¤ã
jQueryRequireJS.pdf
æ¥æ¬èªè¨äºã ã¨ãã®è¾º?
http://zudolab.net/blog/?p=451
è¦ã¯JavaScriptã®ä¾åæ§è§£æ±ºããã¦ãããã©ã¤ãã©ãªã§ããããªæãã§ä½¿ãã¾ãã
require( [ 'lib/a' ,'lib/b' ,'lib/c' ], function(){ // lib/a.js,lib/b.js,lib/c.jsãèªã¿è¾¼ã¾ãã¦ãããã¨ãä¿è¨¼ããã¦ããcallback require( [ 'lib/d' // lib/a.jsã«ä¾åãã¦ããã©ã¤ãã©ãª ,'lib/f'// lib/b.jsã«ä¾åãã¦ããã©ã¤ãã©ãª ], function(){ // lib/a.js,lib/b.js,lib/c.js,lib/d.js,lib/e.jsãèªã¿è¾¼ã¾ãã¦ãããã¨ãä¿è¨¼ããã¦ããcallback }); } );
callbackã®ä¸ã§ãªãã©ã¤ãã©ãªãèªã¿è¾¼ã¾ãã¦ãåæã§ã¢ããªã±ã¼ã·ã§ã³ã³ã¼ããæ¸ãããã
callbackã®ä¸ã§requireé¢æ°ãå¼ã¹ã°ãããã©ã¤ãã©ãªã«ä¾åããå¥ã®ã©ã¤ãã©ãªã«ä¾åãã
ã¢ããªã±ã¼ã·ã§ã³ã¨ããæ¸ãã¡ããã¾ãããã¨ããæãã
ã¨è¨ã£ã¦ããããã¯ããªãåç´ãªä½¿ãæ¹ã§ãã¢ããªã±ã¼ã·ã§ã³ã³ã¼ãã®è¦æ¨¡ã
å°ããå ´åã§ãªãã¨å®ç¨çã§ã¯ãªãã¨æãã
RequireJS moduleã£ã¦ä½?
ããã§ãmoduleã§ããã
define({})
ä¾ãã°ããããªå
容ã®hoge.jsãé
ç½®ããã¦ããã¨ããã
ãã®hoge.jsãhoge moduleã
define({ hoge : 5 ,fuga : 'piyo' });
requireã®ç¬¬ä¸å¼æ°ã«ä¾åãã¦ããmoduleåã®é
åã渡ãã¨ã
ãã®é ã«callbacké¢æ°ã«æ¸¡ãããã
require(['hoge'],function(hoge){ console.log(hoge.hoge); // 5 console.log(hoge.fuga); // 'piyo' });
define(function(){})
moduleã¯defineé¢æ°ã«ãªãã¸ã§ã¯ãã渡ãã ãã§ãªãã
ãªãã¸ã§ã¯ããè¿ãé¢æ°ã渡ãå½¢ã§ãå®ç¾©ã§ããã
ããã¯foo.js(foo module)
define(function(){ return { bar : 6 ,baz : 7 } });
å ç¨ã®hoge moduleã¨ãããã¦ä½¿ãã¨
require(['hoge','foo'],function(hoge,foo){ console.log(foo.bar); // 6 console.log(hoge.fuga); // 'piyo' });
ä¾åé¢ä¿ã®é£é
RequireJS moduleã§ã¯defineé¢æ°ã®ç¬¬ä¸å¼æ°ã«moduleåã®é
åã渡ããã¨ã§ã
moduleãä¾åããmoduleãæå®ãããã¨ãã§ããã
hage.js(hage module)
define(['hoge','foo'],function(hoge,foo){ return { bar : foo.bar ,baz : foo.baz ,hoge : hoge.hoge ,fuga : hoge.fuga } });
ä¸è¨ã®require(['hage'])ã§ã¯hage moduleããæå®ãã¦ããªããã
ä¾åå
ã®hoge,fooãèªã¿è¾¼ã¾ãã¦ããã
hage moduleã使ããã人ã¯hage moduleãä½ã«ä¾åãã¦ãããç¥ã£ã¦ããå¿
è¦ã¯ãªãã
require(['hage'],function(hage){ console.log(hage.bar); // 6 console.log(hage.fuga); // 'piyo' })
require(['hage'])ãå¼ãã å ´åã以ä¸ã®ãããªé ã§å¦çãå®è¡ãããã
- hage.jsãèªã¿è¾¼ã¾ãã
- hoge.jsãèªã¿è¾¼ã¾ãã(hoge moduleãå®ç¾©ããã)
- foo.jsãèªã¿è¾¼ã¾ãã
- foo.jsã§defineé¢æ°ã«æ¸¡ãã¦ããcallbackãå®è¡ãããæ»ãå¤ãfoo moduleã¨ãã¦å®ç¾©ããã
- hage.jsã§defineé¢æ°ã«æ¸¡ãã¦ããcallbackã(hoge,foo moduleã使ç¨ãã¦)å®è¡ãããæ»ãå¤ãhage moduleã¨ãã¦å®ç¾©ããã
- require(['hage'])ã®callbackã«hage moduleã渡ããå¼ã³åºããã
defineé¢æ°ã®callbackã¯moduleã®åæåç¨ã§ãã£ã¦ã
ãã®moduleãä¾åmoduleã¨ãã¦åãã¦å¼ã°ããæã ãå®è¡ãããã
ãã®ä¾åé¢ä¿ã®é£éã¯ä½æ®µéã§ãã§ããã
é¢æ°ã»class
ä»ã¾ã§ã®ä¾ã§ã¯moduleã¨ãã¦é£æ³é
åããå®ç¾©ãã¦ããªãã£ããã
JavaScriptã§æ±ãããã¹ã¦ã®å¤ãmoduleã¨ãã¦å®ç¾©ã§ããã
ä¾ãã°ãããªvery-useful-function.jsãããã°ã
define(function(){ function veryUsefulFunction(){ /* * ä½ããããå®è£ */ } return veryUsefulFunction; });
ããã使ããã人ã¯ã
require(['very-useful-function'],function(veryUsefulFunction){ /* * ä½ãèªåã®ã³ã¼ã */ veryUsefulFunction(); });
ã¨ã
define(['very-useful-function'],function(fun){ /* * ä½ãèªåã®ã³ã¼ã */ fun(); return module; });
ã¨ãæ¸ãã¦ããã°ãããã
ãããªvery-useful-class.jsãããã°ã
define(function(){ function VeryUsefulClass(){ } VeryUsefulClass.prototype.benri = function(){ /* * ä½ããããå®è£ */ }; return VeryUsefulClass; });
ãããªé¢¨ã«æ¸ãã¦ããã°ãã
require(['very-useful-class'],function(Useful){ var hogehoge = new Useful(); hogehoge.benri(); });
ããããRequireJSã®moduleã¨ãã¦é¢æ°ãclassãæä¾ãã¦ãããã
ã°ãã¼ãã«ã¯å
¨ãæ±æãã¦ããªãã
ã¾ã¨ã
ä¾åé¢ä¿ã®é£éã¨é¢æ°ã»classã®æä¾ãªã©ãçµã¿åããã¦ã
ã¢ããªã±ã¼ã·ã§ã³ã³ã¼ããå°åãã«ãã¦ããã°ã
JavaScriptã¢ããªã±ã¼ã·ã§ã³ã®ã³ã¼ãè¦æ¨¡ã大ãããªã£ã¦ã
ã¹ãã²ããã£åãéããããã®ã§ã¯ãªããã¨æå¾
ãã¦ããã
ããã£ã¨æ·±ãã¨ãããææ¡ããä¸ã§ãjava-ja.js#2ã§çºè¡¨ãããã¨æãã