> ±à³Ì¿ª·¢ > AJAXÏà¹Ø >
¿ª·¢Ê·ÉÏ×îÇ¿Ä£¿é¼ÓÔع¤¾ß 2017-01-28 18:16¡¡³ö´¦£ºÇåÆÁÍø¡¡ÈËÆø£º¡¡
Õâ´ÎÒª¼Ç¼µÄÊÇÒ»¸öºÜ¼òµ¥µÄµ«ÊÇ»ù±¾·ûºÏAMD¹æ·¶µÄä¯ÀÀÆ÷¶ËÄ£¿é¼ÓÔع¤¾ßµÄ¿ª·¢Á÷³Ì¡£ÒòΪ×Ô´ÓʹÓùýrequire.js¡¢webpackµÈÄ£¿é»¯¼ÓÔع¤¾ßÖ®ºó¾ÍÒ»Ö±¶ÔËüµÄʵÏÖÔÀíºÜºÃÆ棬ÓÚÊÇÉÔ΢Ñо¿ÁËһϡ£
ʵÏֵķ½·¨ÓÐÐí¶à£¬µ«¼òµ¥ÊµÏֵĻ°´óÖ¶¼»áÓÐÕâÑù¼¸¸ö¹ý³Ì£º
1ʵÏÖÄ£¿éµÄ¼ÓÔØ¡£´ÓÖ÷Ä£¿é˵Æð£¬ÎÒÃÇÐèҪͨ¹ýÒ»¸öÈë¿ÚÀ´¼ÓÔØÎÒÃǵÄÖ÷Ä£¿éµÄÒÀÀµÄ£¿é£¬Í¬Ê±ÔÚ¼ÓÔØÍêÖ÷Ä£¿éÒÀÀµÖ®ºó£¬Äܹ»È¡µÃËù¸÷ÒÀÀµÄ£¿éµÄ·µ»ØÖµ£¬²¢½«ËüÃÇ´«ÈëÖ÷Ä£¿é´úÂëÖУ¬ÔÙÈ¥Ö´ÐÐÎÒÃǵÄÖ÷Ä£¿é´úÂë¡£º¯ÊýÈë¿ÚÀàËÆÓÚÕâÑùµÄÐÎʽ£º
require([ dependents ], function( ){ // Ö÷Ä£¿é´úÂë })ÖÁÓÚÈçºÎÈ¥¼ÓÔØÎÒÃǵÄÒÀÀµÄ£¿é£¬ÕâÀïÒ»°ã¿ÉÒÔÓÐÁ½ÖÖ´¦Àí·½Ê½£¬Ò»ÖÖÊÇͨ¹ýAjaxÇëÇóÒÀÀµÄ£¿é£¬Ò»ÖÖÊÇΪÒÀÀµÄ£¿é¶¯Ì¬´´½¨ script ±êÇ©¼ÓÔØÒÀÀµÄ£¿é£¬ÔÚÕâÀïÎÒÑ¡ÔñµÚ¶þÖÖ·½Ê½£¬²»¹ýÈç¹ûÄãÐèÒª¼ÓÔØÎı¾Îļþ»òÕßJSONÎļþµÄ»°£¬½¨ÒéÄ㻹ÊDzÉÓÃAjax¼ÓÔصķ½Ê½£¬ÖÁÓÚΪʲô£¬½ÓÏÂÀ´»á˵Ã÷£¬µ«ÕâÀïΪÁ˼òµ¥´¦ÀíÎÒÃDz»¿¼ÂÇÕâÖÖÇé¿ö¡£
ËùÒÔÎÒÃÇ»á±éÀúÖ÷Ä£¿éµÄÒÀÀµÊý×飬¶ÔÒÀÀµÄ£¿éµÄ·¾¶×ö¼òµ¥µÄ´¦ÀíÖ®ºó£¬¶¯Ì¬´´½¨ script ±êÇ©¼ÓÔØÿһ¸öÒÀÀµÄ£¿é¡£ËùνµÄ¼ÓÔØÄ£¿é£¬Æä±¾ÖʱãÊÇͨ¹ýÍøÂçÇëÇó½«Ä£¿é Fetch µ½±¾µØ¡£Í¬Ê±ÎÒÃÇ»¹ÐèÒªÖªµÀͨ¹ý script ±êÇ©¼ÓÔØ×ÊÔ´µÄÁ½¸öÌص㣺
1.1 script ±êÇ©¼ÓÔص½JS´úÂëÖ®ºó»áÁ¢¼´Ö´ÐÐÕâÒ»¶Î´úÂë¡£JSONPÒ²ÀûÓÃÁË script ±êÇ©µÄÕâ¸öÌØÐÔ¡£
1.2 ¿ÉÒÔͨ¹ý script.onload ºÍ script.onerror ¼àÌýÄ£¿éµÄ¼ÓÔØ×´¿ö¡£
2ʵÏÖÄ£¿éµÄ¶¨Òå¡£ÔÚAMD¹æ·¶ÖУ¬Ã¿Ò»¸öÄ£¿éµÄ±àдÎÒÃÇÐèÒª×ñÑÀàËÆÓÚÕâÑùµÄÐÎʽ£º
define([ dependents ], factory)ÉÏÃæҲ˵µ½£¬script ±êÇ©»áÁ¢¼´Ö´ÐÐËù¼ÓÔسɹ¦µÄÄ£¿é£¬ËùÒÔÈç¹ûÔÚ´Ë֮ǰÎÒÃÇµÄ define º¯Êý±»¹ÒÔص½È«¾ÖµÄ»°£¬define º¯Êý»á±»´«Èë ([ dependents ], factory) µÈ²ÎÊýºóÖ´ÐУ¬ÒÔÍê³ÉÄ£¿éµÄ¶¨Ò幤×÷¡£
¹ØÓÚÄ£¿é¶¨ÒåµÄ¸ÅÄîÕâÀïÐèҪ˵һÏ£¬ÎÒÃǵÄÄ£¿é¶¨Ò壬ÊÇÖ¸³É¹¦½«Ä£¿éµÄ·µ»ØÖµ£¨»òÕ߸ÃÄ£¿éµÄÈ«²¿´úÂ룩 cache µ½ÎÒÃǵı¾µØ»º´æµ±ÖУ¬ÎÒÃÇ»áʹÓÃÒ»¸ö±äÁ¿¸ºÔðÈ¥»º´æËùÓеÄÒÀÀµÄ£¿éÒÔ¼°ÕâЩÒÀÀµÄ£¿éËù¶ÔÓ¦µÄÄ£¿éID£¬ËùÒÔÿ´ÎÔÚÖ´ÐÐ require ·½·¨»òÕß define ·½·¨Ö®Ç°ÎÒÃǶ¼»áÈ¥¼ì²éÒ»ÏÂËùÒÀÀµµÄÄ£¿éÔÚ»º´æÖÐÊÇ·ñ´æÔÚ£¨¸ù¾ÝÄ£¿éID²éÕÒ£©£¬¼´ÊÇ·ñÒѾ³É¹¦¶¨Òå¡£Èç¹ûÒѾ³É¹¦¶¨Òå¹ýÁË£¬ÎÒÃDZã»áºöÂÔ¶Ô´ËÄ£¿éµÄ´¦Àí£¬·ñÔò¾Í»áÈ¥µ÷Óà require ·½·¨¼ÓÔز¢¶¨ÒåËü¡£´ýËùÒÀÀµÄ£¿éÊý×é¶ÔӦȫ²¿Ä£¿é¶¼¼ì²éÒѾ³É¹¦¶¨Òå¹ýÖ®ºó£¬ÎÒÃÇÔÙ´Ó»º´æÖÐÈ¡³öÕâЩÒÀÀµÄ£¿éµÄ·µ»ØÖµ´«Èë factory ·½·¨µ±ÖÐÖ´ÐÐÖ÷Ä£¿é»òÕß cache ÎÒÃǵ±Ç°¶¨ÒåµÄÄ£¿é¡£
ÒÔÉϾÍÊÇÒ»¸ö¼òµ¥µÄÄ£¿é¼ÓÔØÆ÷µÄÒ»°ãÔÀíÁË£¬¾ßÌåϸ½ÚÔÙÔÚÏÂÃæ¾ßÌå˵Ã÷¡£
ËùÒÔÎÒÃǵĹؼüÊÇʵÏÖ require ºÍ define ·½·¨¡£²»¹ýÔÚÕâÀïÓÐÒ»¸öÖØÒªµÄϸ½ÚÐèÒªÎÒÃÇ´¦Àí£¬Ç°ÃæÓÐÌáµ½¹ý£¬ÎÒÃǵÄÿһ´Î require »òÕß define ֮ǰ»áÈ¥¼ì²éËùÒÀÀµÄ£¿éÊÇ·ñ¶¼ÒѾÍêÈ«¶¨Ò壬ÔÙÈ¥¶¨Òå䶨ÒåµÄÒÀÀµÄ£¿é£¬ÄÇÈç¹ûËùÓеÄÒÀÀµÄ£¿é¶¼ÒѾȫ²¿Íê³É¶¨Ò壬ÎÒÃÇµÄ require »òÕß define ÔõôÑù²ÅÄܼ´Ê±µÄÖªÏþµ½ÕâÒ»Ç鱨ÄØ£¿
ÎÒÃÇ¿ÉÒÔ½èÖúÓÚʵÏÖÒ»¸öÀàËÆÓÚ Nodejs µ±ÖÐ EventEmiter Ä£¿éµÄʼþ·¢ÉäÆ÷È¥Íê³ÉÎÒÃǵÄÐèÇó¡£Õâ¸öʼþ·¢ÉäÆ÷ÓÐÁ½¸öÖ÷ÒªµÄ·½·¨ watch ºÍ emit¡£
watch£ºÎÒÃÇÔÚ¼ÓÔØÒÀÀµÄ£¿éµÄͬʱ£¬½«ÎÒÃǵÄÒÀÀµÄ£¿éÊý×éºÍ»Øµ÷º¯Êý´«Èëʼþ·¢ÉäÆ÷µÄ watch ·½·¨£¬watch ·½·¨»á´´½¨Ò»¸öÈÎÎñ£¬¼àÌýËù´«ÈëÒÀÀµÄ£¿éÊý×éµÄ¼ÓÔØ×´¿ö£¬²¢ÇÒµ±¼ì²âµ½ËùÒÀÀµÄ£¿éÊý×éÄ£¿éÈ«²¿¶¨Òå³É¹¦Ö®ºóÖ÷¶¯´¥·¢Ö®Ç°´«ÈëµÄ»Øµ÷º¯Êý£¬Ö´ÐнÓÏÂÀ´µÄÂß¼¡£
emit £ºÃ¿´ÎÓÐÄ£¿é±»¶¨Òå³É¹¦£¬±ã»áµ÷ÓÃʼþ·¢ÉäÆ÷µÄ emit ·½·¨·¢ËÍÒ»¸öÄ£¿é¶¨Òå³É¹¦µÄÐźţ¬Í¬Ê±Ê¼þ·¢ÉäÆ÷»áµ÷Óà deal_loaded ·½·¨¼ì²éÒ»±éµ±Ç°¶¨Òå³É¹¦µÄÒÀÀµÄ£¿éËùÔÚÒÀÀµÄ£¿éÊý×éÊÇ·ñÈ«²¿¶¨Òå³É¹¦£¬Èç¹ûÊǵĻ°£¬ÔÙÈ¥µ÷Óà excute ·½·¨Ö´Ðе±Ç°ÍêÈ«¶¨Òå³É¹¦µÄÒÀÀµÄ£¿éÊý×éËù¶ÔÓ¦µÄ»Øµ÷º¯Êý¡£
ʼþ·¢ÉäÆ÷µÄ´úÂëÈçÏ£º
var utils = { ...... ¡¡¡¡¡¡¡¡¡¡¡¡ proxy : (function(){ var tasks = { } var task_id = 0 var excute = function( task ){ console.log( "excute task" ) var urls = task.urls var callback = task.callback var results = [ ] for( var i = 0; i < urls.length; i ++ ){ results.push( modules[ urls[ i ] ] ) } callback( results ) } var deal_loaded = function( url ){ console.log( "deal_loaded " + url ) var i, k, sum = 0 for( k in tasks ){ if( tasks[ k ].urls.indexOf( url ) > -1 ){ for( i = 0; i < tasks[ k ].urls.length; i ++ ){ if( m_methods.isModuleCached( tasks[ k ].urls[ i ] ) ){ sum ++ } } if( sum == tasks[ k ].urls.length ){ excute( tasks[ k ] ) delete( tasks[ k ] ) } } } } var emit = function( m_id ){ console.log( m_id + " was loaded !" ) deal_loaded( m_id ) } var watch = function( urls, callback ){ console.log( "watch : " + urls ) var sum for( var i = 0; i < urls.length; i ++ ){ if( m_methods.isModuleCached( urls[ i ] ) ){ sum ++ } } if( sum == urls.length ){ excute({ urls : urls, callback : callback }) } else { console.log( "´´½¨¼àÌýÈÎÎñ £º " ) var task = { urls : urls, callback : callback } tasks[ task_id ] = task task_id ++ console.log( task ) } } return { emit : emit, watch : watch } })( ) }
¡¡