资产管道中的条件JavaScript需求

编程入门 行业动态 更新时间:2024-10-28 15:30:56
本文介绍了资产管道中的条件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 'ajax.googleapis/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 "ajax.googleapis/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需求

本文发布于:2023-07-08 01:08:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1070210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:道中   条件   资产   需求   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!