2013-06-26 4 views
1

회사의 주 웹 사이트에서 AMD 모듈을로드하는 데 RequireJs를 사용하고 있습니다. 우리는 또한 requireJS를 사용하여 별도의 도메인에서 호스팅되는 모듈을로드합니다. requireJS는 "context"-field를 사용하여이를 지원합니다. 즉. 별도의 baseUrl을 사용하여 별도의 샌드 박스 환경을 만듭니다.컨텍스트 간의 다중 컨텍스트 요구 사항 및 상속 종속성

문제는 두 개의 개별 컨텍스트가 공통 개체를 공유하도록하려는 경우입니다. 예. jQuery (두 번로드하는 것을 피하기 위해) 또는 위젯 사이에서 이벤트를 트리거하는 pubsub 구현. http://example.com/src/main.js

require(['jquery'], functin($){ 

    // create sandbox/contexted instance of requireJs 
    var sandbox = requireJS.config({ 
     context: "myContext", 
     baseUrl : 'http://otherdomain.com/src' 
    }); 

    // load module (with internal dependencies) from other domain 
    sandbox.require(['modules/bootstrap.js'], function(bootstrap){ 
     bootstrap.run(); 
    }); 

}); 

bootstrap.js에서 호스팅 -

가 나는 글로벌 requireJS와 함께

main.js를 함수를 정의하여, 컨텍스트 사이에 모듈을 삽입 할 수있는 방법을 생각했습니다 - 예 : http://widgets.com/src/modules/bootstrap.js

define(function(){ 

    // define jquery into sandboxed environemnt 
    requireJs('jquery', function($) { 
     define('jquery', function(){ 
      return window.jQuery; 
     }); 
    }); 

    // obtain sandboxed instance from parent 
    var sandbox = requireJs.config({ 
     context: "myContext" 
    }); 

    sandbox(['jquery'], function($){ 
     console.log($); 
    }); 

}); 

문제에 호스팅입니다 임 정의 JQuery와 (또는 함수를 반환하는 모든 다른 모듈), tje "requreJS"웨이는 (전역를 사용하지 않는) 항상 오류

// define jquery into sandboxed environemnt 
    requireJs('jquery', function($) { 
     define('jquery', $); 
    }); 
가 발생합니다 경우

이것은 버그 또는 기능입니까?

답변

0

Ive는 실제로이 문제를 직접 해결했습니다. 트릭은 새로운 익명 함수 내에서 반환 된 함수를 래핑하는 것입니다.

// inject global module into contexted require 
function injectContext(moduleId, module){ 
    var m = module; 
    if(typeof module === 'function') { 
     m = function() { 
      return module; 
     }; 
    } 
    define(moduleId, m); 
} 
관련 문제