ユーザーが自分のサイトに埋め込むことができる JavaScript ファイルがあります。これは、coffeescript と Rails アセット パイプラインを使用して、一連のファイルを 1 つにコンパイルします。
# 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が役立つと思いましたが、そうではないと思います。
ありがとう!