资产管道中的条件 javascript require

发布于 2024-12-10 02:26:23 字数 682 浏览 1 评论 0原文

我正在为资产管道而苦苦挣扎。我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯了 2024-12-17 02:26:23

我建议您使用 yepnope,一个轻量级库,用于并行加载此类库(以提高速度),它使您可以选择运行一些其他代码来测试库是否已加载。例如:(


yepnope([{
  load: 'http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js',
  complete: function () {
    if (!window.jQuery) {
      yepnope('asset_path('you_local_copy_of_dojo') ');
    }
  }
}])

注意:您将需要 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:


yepnope([{
  load: 'http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js',
  complete: function () {
    if (!window.jQuery) {
      yepnope('asset_path('you_local_copy_of_dojo') ');
    }
  }
}])

(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.

最冷一天 2024-12-17 02:26:23

谢谢理查德!

我不想让 yepnope 加载一个库。在我看来,这太过分了。这是我根据您的帮助提出的解决方案(用 slim 编写):


1/ 在vendors/assets/javascripts/ 中,我有我的 dojo.js。

2/ 在 config/application.rb 中:

# Precompile these assets files
config.assets.precompile += ['dojo.js']

3/ 在模板中:

= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/dojo/#{Settings.dojoVersion}/dojo/dojo.xd.js", :'data-dojo-config' => %Q(dojoBlankHtmlUrl:'/blank.html', baseUrl: 'assets/', modulePaths: {custom: 'javascripts/modules'})
script = "typeof(dojo) === \"undefined\" && document.write(unescape('%3Cscript src=\"#{asset_path('dojo')}\"%3E%3C/script%3E'));".html_safe

我还在 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:

# Precompile these assets files
config.assets.precompile += ['dojo.js']

3/ In the template:

= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/dojo/#{Settings.dojoVersion}/dojo/dojo.xd.js", :'data-dojo-config' => %Q(dojoBlankHtmlUrl:'/blank.html', baseUrl: 'assets/', modulePaths: {custom: 'javascripts/modules'})
script = "typeof(dojo) === \"undefined\" && document.write(unescape('%3Cscript src=\"#{asset_path('dojo')}\"%3E%3C/script%3E'));".html_safe

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文