2012-12-14 2 views
0

사용자가 자신의 사이트에 삽입 할 수있는 자바 스크립트 파일이 있습니다. coffeescript와 Rails 자산 파이프 라인을 사용하여 여러 개의 파일을 하나의 파일로 컴파일합니다.임베디드 위젯 용 자바 스크립트 캡쳐

# The following dependencies will be compiled into test.js (rails asset pipeline manifest file) 
# 
#= require jquery 
#= require jquery.cookie 

jQuery -> 
    $.cookie('foo', 'bar') 
    console.log $.cookie('foo') 

샘플 페이지 예상대로이 작동 :

<!DOCTYPE html> 
    <head></head> 
    <body> 
    <script src="http://localhost:3000/assets/test.js" type="text/javascript"></script> 
    </body> 
</html> 

그러나 어떤 사이트에 임베드해야하므로이 스크립트는 위젯입니다. 사용자가 그 아래에 자신의 jQuery를 추가하는 경우, 다음 일이 휴식 :

<!DOCTYPE html> 
    <head></head> 
    <body> 
    <script src="http://localhost:3000/assets/test.js" type="text/javascript"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    </body> 
</html> 

이 오류 Uncaught TypeError: Object function (a,b){return new e.fn.init(a,b,h)} has no method 'cookie'

그래서 자기가 포함되어 test.js 모든 것을 마무리하는 가장 좋은 방법은 무엇을 주는가? 익명의 함수로 모든 것을 래핑하는 coffeescript가 여기에서 도움이 될 수 있다고 생각했지만 그렇지 않은 것 같습니다.

감사합니다.

답변

0

이 튜토리얼은 몇 가지 단서를 제공한다 : 나는 익명 함수 (jquery.cookie 같은) 플러그인에 대한 축소 된 소스 코드를 포함해야 할 수도 있습니다처럼 http://alexmarandon.com/articles/web_widget_jquery/

그것은 소리. 애셋 파이프 라인은 파일 맨 위에 있지 않으면 (첫 번째 주석 블록에서) 다음과 같은 명령문을 필요로하지 않습니다. https://github.com/sstephenson/sprockets/issues/398

관련 문제