2012-10-16 2 views
0

다른 모듈을 결합 할 수 있고 조작하고 싶지 않은 제 3 자 라이브러리를 사용하기 때문에 동일한 스크립트를 두 번 이상로드하지 못하도록하고 싶습니다.동일한 자바 스크립트를 두 번 이상로드하지 마세요.

이전에이 작업을 수행 한 사람이 있습니까?

+0

은 서버 측 코드 예 : http ['ScriptManager.RegisterClientScript'] (와 마찬가지로,로드되는 자원을 관리하는 데 사용됩니다 위해 흔히 : //msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerclientscriptblock.aspx) ASP.NET에서. – zzzzBov

답변

0

요구 사항 JS와 같은 라이브러리가 내 문제를 해결하지 못해서 필자는 아래에 게시 할 자체 솔루션을 만들었습니다.

내 시스템 또한 다른 모듈로 만들어졌습니다. 주 모듈에는 모든 모듈 (PHP, JS 및 CSS 파일)의 종속성에 대한 로더가 있습니다. 종속성이로드 된 후 응용 프로그램은 이벤트를 트리거하고 파일의 중복 포함을 방지하는 전역 변수를 설정합니다.

희망이 있습니다. 의심스러운 점이 있으면 알려주세요.

코드 : 그것은 일반적으로 사이트가 구축되는 방식에 따라 달라집니다

//Main 
var main = { 
    init: function(){ 
     //Dependencies to load (php, js or css) 
     var deps = [ 
      '/helpers/edit/v/edit.php',     
      '/helpers/edit/css/edit.css',    
      '/helpers/validate/js/jquery.validate.js,messages_pt_BR.js' 
     ];   
     //Load initial pack 
     if (!window.editReady){ 
      //Load dependencies 
      this.load('edit',deps);   

      //Bind loaded event 
      $('body').on('editReady',function(){ 
       //Set editLoaded to avoid double ajax requests 
       window.editReady = true; 

       //Do whatever you need after it's loaded 

      }); 
     } 
    }, 
    //Load external resources 
    load: function(name,data_urls){ 
     var url, ext; 
     var len = data_urls.length; 
     var i = 0; 
     $(data_urls).each(function(){ 
      //Get proper file 
      $.get(this, function(data) { 
       url = this.url; 
       ext = url.split('.').pop(); 
       switch(ext){ 
        case 'php': 
         this.appended 
         $(data).appendTo('body'); 
         break; 
        case 'css': 
         $('<link/>') 
         .attr({ 
          'rel':'stylesheet', 
          'href':url 
         }).appendTo('head'); 
         break; 
       } 
       //Check if all files are included 
       i += 1; 
       if (i == len) { 
       $("body").trigger(name+"Ready"); 
       } 
      }); 
     }); 
    } 
}; 

var modules = { 
    themes : { 
     init : function(){ 
      //Load dependencies 
      var deps = [ 
       '/helpers/plupload/js/plupload.js,plupload.html5.js,plupload.flash.js' 
      ];   
      if (!window.themesReady){ 
       //Set themesReady to avoid double ajax requests 
       window.themesReady = true; 

       //Load dependencies 
       main.load('themes',deps); 

       $('body').on('themesReady',function(){ 

        //Do whatever you need after it's ready 
       }); 
      } 
     } 
    } 
}  
main.init(); 
4

RequireJS? 당신이 찾고있는 것 같아요.

관련 문제