2017-04-13 8 views
0

JavaScript 요구 사항 내에서 Intl-Tel-Input 라이브러리에 액세스하는 방법을 찾으려합니다. 나는 다음 코드를 가지고 있으며 타사 코드에 의해 액세스되고 있기 때문에 requireJS 모듈 외부에 콜백이 필요하다.타사에서로드 한 requireJS 패키지의 JavaScript 액세스

callback 함수 내에서 intlTelInput 코드에 액세스하려면 어떻게해야합니까? 내 나뭇 가지 템플릿에서

:

<script type="text/javascript"> 
    require(['crmpicco/details'], function(details) { 
     details.init(); 
    }); 
    var callback = function (response) {    
     // I want to access intlTelInput in here  
    }; 
</script> 

config.js이있다 : 내가 알 수있는 바와 같이

require = { 
    baseUrl: '/assets/js', 
    paths: { 
     'intl-tel-input': '/assets/vendor/intl-tel-input/build/js/intlTelInput.min', 
    }, 
    shim: { 
     'intl-tel-input': { 
      deps: ['libphonenumber-utils'] 
     }, 
    } 
}; 
+0

그들은 UMD를 지원하기 때문에 require ([ 'intl-tel-input'], function (IntlTelInput) {};'콜백 함수를 사용할 수 있다고 생각합니다. 그리고 shimming 필요가 없습니다. – Andrey

+0

@Andrey 구현 방법을 알고 있습니까? 'callback'이'require' 안에 있다면 제 3 자 코드는 콜백 함수를 찾을 수 없습니다. – crmpicco

+0

@Andrey 비슷하게'require'가'callback' 안에 있다면'intlTelInput'을 찾을 수 없습니다. 'TypeError : intlTelInput 함수가 아닙니다'. – crmpicco

답변

0

하면, 가 살펴주십시오 조각 같은 뭔가가 필요

<head> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.3/require.min.js"></script> 
 
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/11.0.10/css/intlTelInput.css" /> 
 
    <script> 
 
    require.config({ 
 
     paths: { 
 
     'intl-tel-input': 'https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/11.0.10/js/intlTelInput', 
 
     'jquery': 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min' 
 
     } 
 
    }); 
 
    </script> 
 
</head> 
 
<body> 
 

 
    <input id="some-input" /> 
 
    <script> 
 
    require(['intl-tel-input'], function() { 
 
     function someCallback(){ 
 
     $('#some-input').intlTelInput(); 
 
     } 
 

 
     setTimeout(someCallback, 1000); 
 
    }); 
 
    </script> 
 
</body>

+0

의견을 주셔서 감사합니다. 여기서'setTimeout'의 목적은 무엇입니까? 위 내 의견을 보셨습니까? – crmpicco

+0

@crmpicco, setTimeout은 비동기 콜백 내부에서 intlTelInput을 초기화 할 수 있다는 것을 보여주기위한 것입니다. – Andrey

+0

불행히도 'intl-tel-input'의'require '내부에서 수행 할 수 없습니다. Google의 보이지 않는 reCaptcha 데모에서 출처 (https://www.google.com/recaptcha/api2/demo?invisible=true)를 확인하십시오. – crmpicco