2013-02-18 3 views
0

jQuery가 require.js에 연결되도록하려면 require.js 뒤에 jQuery를로드해야합니다. 그건 잘 작동하지만, 일부 이상한 AMD 물건 (see bottom of BlockUI source)을 수행하는 BlockUI라는 jQuery 플러그인을 포함하자마자 불일치 오류가 발생합니다. 불일치 오류가 전체 소스를 뱉어 내고 BlockUI를 제거하면 모든 것이 작동하기 때문에 BlockUI가 원인임을 알 수 있습니다. require.js를 사용하는지 여부는 중요하지 않습니다. 도서관 만 포함하면 충분합니다. 모든 스크립트가 올바른 순서로로드되었는지 확인하려면 왼쪽에있는 리소스 탭에 이상 http://goo.gl/4tYE7Require.js + jQuery + BlockUI가 일치하지 않는 오류를 발생시키는 이유는 무엇입니까?

스위치 :

이 jsfiddle을 참조하십시오. 문제 없다? 좋아, 이제 콘솔을 엽니 다. 포로! 피가 도처에!

Require.js 또는 BlockUI 버그 (BlockUI가 약간 구식이기 때문에 BlockUI에 돈을 지불했는지)가 확실하지 않지만 이에 대한 해결 방법이 있습니까? BlockUI의 맨 아래에서 실제로 무엇이 일어나고 있습니까? jQuery는 소스 맨 아래에 AMD 모듈로 자체를 노출합니다. 왜 BlockUI가 비슷한 일을합니까?

WordUI 플러그인 (Jigoshop)이로드했기 때문에 BlockUI의로드를 제어 할 수 없기 때문에이 기능을 사용하는 것이 중요합니다.

답변

3

어떻게 BlockUI를 사용하고 있습니까? 당신이 그것을 요구하거나 의존성으로서 그것을 사용하고 있습니까? RequireJS 사이트에서 촬영 : 당신이 모듈로 jQuery를하고 BlockUI를 사용하는 경우

If you manually code a script tag in HTML to load a script with an anonymous define() call, this error can occur.

Also seen if you manually code a script tag in HTML to load a script that has a few named modules, but then try to load an anonymous module that ends up having the same name as one of the named modules in the script loaded by the manually coded script tag. Finally, if you use the loader plugins or anonymous modules (modules that call define() with no string ID) but do not use the RequireJS optimizer to combine files together, this error can occur.

The optimizer knows how to name anonymous modules correctly so that they can be combined with other modules in an optimized file. To avoid the error: Be sure to load all scripts that call define() via the RequireJS API. Do not manually code script tags in HTML to load scripts that have define() calls in them. If you manually code an HTML script tag, be sure it only includes named modules, and that an anonymous module that will have the same name as one of the modules in that file is not loaded. If the problem is the use of loader plugins or anonymous modules but the RequireJS optimizer is not used for file bundling, use the RequireJS optimizer.

http://requirejs.org/docs/errors.html

그냥 잘 작동합니다 :

require.config({ 
    'paths': { 
     'jquery': 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min', 
     'blockUI': 'http://malsup.github.com/jquery.blockUI'  
    }, 
    'shim': { 
     'blockUI': ['jquery']  
    } 
}) 

require(['blockUI'], function(){ 
    // Don't return a value to the callback as it just augments the $.fn 
    console.log($.fn.block); 
    console.log($.fn.unblock); 
}); 

예 - http://jsfiddle.net/Blink/ae4nr/

What is actually happening in the bottom of BlockUI? jQuery exposes itself as an AMD module at the bottom of the source, why does BlockUI do similar things?

그것은이다 플러그인 등으로 AMD를 지원하는 알려진 기술. 함수에 코드를 두는 것은 AMD가 존재하지 않으면 window.jQuery이 전달 될 수 있다는 것을 의미합니다. 그렇지 않으면 함수는 define으로 전달 될 수 있고 jQuery가로드되면 전달됩니다. 어쨌든 모든 플러그인 코드는 $로 사용할 jQuery 객체를 받는다.

define(['jquery'], setup); 
+0

내가 어떻게 jQuery를 통해 많은 제어 할 수 없습니다 : 경로로 'jQuery를'정의

안전하게 'JQuery와'정의 될 것이라고 가정 할 수있다 AMD 너무 플러그인 작성자 그것을 사용의 요구 사항입니다 및 BlockUI가로드됩니다. 나는 WordPress (큰 팬은 아니지만)를 사용하고 있으며, Jigoshop이라는 플러그인은 다른 스크립트와 함께 BlockUI 스크립트 태그를 헤더에 자동으로 추가합니다. jQuery는 Wordpress에서도 자동으로로드됩니다. jsfiddle 내가 연결되어 꽤 많이 내 헤더에 무슨 일이있다. 나는 더 많은 통제가 있었으면 좋겠지 만, 불행히도 나는하지 않는다. 결국, 내 스크립트가 바닥 글에 배치되기 때문에 헤더에서 어떤 일이 일어나는지 상관하지 않습니다. 내 스크립트가 작동하려면 jQuery를로드해야한다. 그 때 일이 지저분 해지면 ... – qwerty

+0

... require.js가 종속성으로 jQuery를 사용하려면 jQuery보다 먼저 require를 포함해야하지만 jquery 앞에 require.js가 포함되어 있으면 헤더의 다른 모든 항목 (BlockUI, for 예를 들어) 오류를 일으키는 자체를 연결하려고합니다. footer에 require.js를로드 할 수 있고 다른 모듈을로드하기 위해 어떻게 든 jQuery에 의존한다면, 좋을 것입니다. 내 문제가 보이니? – qwerty

+0

플러그인을 수정할 수 있습니까? 스크립트 삽입을 꺼내서 require를 통해 직접로드 하시겠습니까? 왜 당신은 전혀 요구하지 않는가?당신의 테마/플러그인이 만들어지는 방식과 같은 소리는이 patten에 잘 맞지 않습니다. –