나는 공식 API는 당신이 원하는 지원 생각하지만, 여기에 해결 방법에 대한 몇 가지 아이디어입니다하지 않습니다
옵션 1 : CDN이 사용되는 랜덤 섹션을 config (설정) 경로에 삽입 식입니다. 다음은 jQuery를위한 예입니다하지만 당신은 다른 라이브러리에 대한 비슷한 반복 수 :
<script src="js/lib/require.js"></script>
<script>
var jqCDNs = [ 'http://code.jquery.com/jquery-2.0.2', 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2' ]
require.config({
paths: {
foomodule: 'libs/foo',
// create array with two elements: a random CDN url plus a local fallback version
jquery: [ jqCDNs[Math.floor(Math.random() * jqCDNs.length)], 'jquery-2.0.2' ]
}
});
require(['jquery'], function(jq) {
// do stuff
});
옵션 2 : Fine-grained URL control에 설명 된대로 무시 RequireJS의로드 방법 :
<script src="require.js"></script>
<script>
require.config({
paths: {
foomodule: 'libs/foo',
jquery: [ '{CDN_PREFIX}/jquery-2.0.2' ]
}
});
(function() {
var CDNs = [ 'http://code.jquery.com/', 'http://ajax.aspnetcdn.com/ajax/jQuery/' ]
var load = requirejs.load;
requirejs.load = function (context, moduleId, url) {
var prefix = CDNs[Math.floor(Math.random() * CDNs.length)];
//modify url here, then call original load
url = url.replace('./{CDN_PREFIX}', prefix);
return load(context, moduleId, url);
};
require(['jquery'], function(jq) {
// do stuff
});
}());
</script>
옵션 3 : 쓰기 자신의 Loader Plugin. 아래의 샘플 코드는 완전한 해결책은 아니지만 무슨 뜻인지의 아이디어를 보여줍니다 당신의 팁
// normal RequireJS config stuff
require.config({
paths: {
foomodule: 'libs/foo',
jquery: [ 'http://code.jquery.com/jquery-2.0.2', 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2' ]
}
});
// crud loader plug-in. See http://requirejs.org/docs/plugins.html for more things to think about
//
// RequireJS supports an array of path values but it always takes them in order
// this loader plug-in is a hack to shuffle the order before RequireJS does its request
define('cdn', function() {
//credit: http://stackoverflow.com/a/12646864/151212
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
return {
load: function (name, req, onload, config) {
// if the module has an array of path configs,
if (config.paths && config.paths[name] && config.paths[name].length && config.paths[name].length > 1) {
// shuffle the order of it
shuffleArray(config.paths[name])
}
// and then pass on to the normal RequireJS process
req([name], function (value) {
onload(value);
});
}
};
});
// then use the randPath! prefix before any module that you want to use this shuffle process
require(['cdn!jquery'], function(jq) {
// do stuff
});
고마워요. 나는 그것을 최대한 빨리 지나갈 것이다. 오후 8시 30 분 P.S. 옵션 1은로드되지 않은 모듈의 번호가 있고 사전에 모든 경로를 정의하고 싶지 않기 때문에 수행하지 않지만 다른 2는 정말 재미 있고 작동 할 수 있습니다. – iMoses