2017-04-13 7 views
0

JavaScriptを使用してIntl-Tel-Inputライブラリにアクセスする方法を試しています。私は以下のコードを持っており、第三者のコードがアクセスしているので、requireJSモジュールの外にコールバックが必要です。JavaScriptでサードパーティーロードのrequireJSパッケージにアクセスする

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){}; '内部コールバックを使うことができると思います。シミングの必要はありません。 – Andrey

+0

@Andreyそれをどのように実装すべきか知っていますか? 'callback'が' require'の内部にある場合、サードパーティのコードはコールバック関数を見つけることができません。 – 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

残念ながら、これは 'require'の' intl-tel-input'では実行できません。私が達成しようとしているGoogleの見えないreCaptchaデモのソース、https://www.google.com/recaptcha/api2/demo?invisible=trueを参照してください。 – crmpicco

関連する問題