本文介绍了资产管道中的条件JavaScript需求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在资产管道中苦苦挣扎.我正在从Google CDN加载dojo,并将其放入我的模板中:

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'})

如果在本地运行或CDN已关闭,我只希望回退到本地版本.我想到了这样做:

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'));

但是我不喜欢它,因为它可以在资产管道之外工作.我想将dojo保留在vendors/assets/javascripts/dojo中.我如何获得资产管道提供的后备服务.

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.

是否有一种方法可以在资产管道中声明有条件的需求.我想要的是运行一些javascript测试,并根据结果提供文件.

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.

谢谢

推荐答案

感谢理查德!

我不想让yepnope加载一个库.这将是过分的imo.这是我根据您的帮助(细写)提出的解决方案:

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/在vendor/assets/javascripts/中,我有我的dojo.js.

1/ In vendors/assets/javascripts/, I have my dojo.js.

2/在config/application.rb中:

2/ In 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,这些选项可以处理所有工作.我们会看到的.


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.

这篇关于资产管道中的条件JavaScript需求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:18