资产管道中的条件 javascript require
我正在为资产管道而苦苦挣扎。我正在从 Google CDN 加载 dojo,并将其放入我的模板中:
= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js', :'data-dojo-config' => %Q(dojoBlankHtmlUrl:'/blank.html', baseUrl: 'assets/', modulePaths: {custom: 'javascripts/modules'})
我只想在本地运行或 CDN 关闭时回退到本地版本。我想过这样做:
script typeof(dojo) === "undefined" && document.write(unescape('%3Cscript src="js/libs/dojo-1.6.1.min.js"%3E%3C/script%3E'));
但我不喜欢它,因为它在资产管道之外工作。我想将 dojo 保留在 vendors/assets/javascripts/dojo
中。我怎样才能让资产管道提供后备服务。
有没有办法在资产管道中声明条件要求。我想要的是运行一些 javascript 测试,并根据结果提供一个文件。
谢谢
I'm struggling with the asset pipeline. I'm loading dojo from Google CDN putting this in my template:
= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js', :'data-dojo-config' => %Q(dojoBlankHtmlUrl:'/blank.html', baseUrl: 'assets/', modulePaths: {custom: 'javascripts/modules'})
I just want a fallback to a local version if running locally or if the CDN is down. I thought of doing this:
script typeof(dojo) === "undefined" && document.write(unescape('%3Cscript src="js/libs/dojo-1.6.1.min.js"%3E%3C/script%3E'));
But I don't like it as it works out of the asset pipeline. I want to keep dojo in vendors/assets/javascripts/dojo
. How can I get the fallback to be served by the asset pipeline.
Is there a way do declare conditional require in the asset pipeline. What I want is to run some javascript tests, and depending on the result serve a file.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用 yepnope,一个轻量级库,用于并行加载此类库(以提高速度),它使您可以选择运行一些其他代码来测试库是否已加载。例如:(
注意:您将需要 asset_path 帮助程序周围的 erb 标签)
本地 dojo 文件将位于 assets/javascript 文件夹中,但不包含在应用程序清单中。您需要将dojo文件添加到预编译数组中:
config.assets.precompile += 'your_local_file.js'
这将使 asset_path 帮助程序可用。
I suggest you use yepnope, a lightweight library for loading libraries like this in parallel (for speed) and it gives you the option to run some other code to test if the library is loaded. For example:
(Note: You will need erb tags around the asset_path helper)
The local dojo file would be in the assets/javascript folder, but not included in the application manifest. You need to add the dojo file to the precompile array:
config.assets.precompile += 'your_local_file.js'
And this will make it available to the asset_path helper.
谢谢理查德!
我不想让 yepnope 加载一个库。在我看来,这太过分了。这是我根据您的帮助提出的解决方案(用 slim 编写):
1/ 在vendors/assets/javascripts/ 中,我有我的 dojo.js。
2/ 在 config/application.rb 中:
3/ 在模板中:
我还在 Rails Google Group 上发帖,请求在
javascript_include_tag
中添加两个选项::test 和 :local所有工作。我们拭目以待。Thanks Richard!
I don't want to have yepnope to load one library. It would be overkill imo. Here is the solution I came up with, based on your help (written in slim):
1/ In vendors/assets/javascripts/, I have my dojo.js.
2/ In config/application.rb:
3/ In the template:
I also posted on the Rails Google Group to request the addition of two options to the
javascript_include_tag
, :test and :local that would take care of all the work. We'll see.