如何使用 Sprockets 架构在 Rails 3.1 中的 JQuery 插件中引用文件?

发布于 2024-11-09 06:18:30 字数 1498 浏览 3 评论 0原文

Plupload 插件就是一个很好的例子。以下是添加到 vendor 目录中的插件的列表:

./plupload/jquery.plupload.queue
./plupload/jquery.plupload.queue/css
./plupload/jquery.plupload.queue/css/jquery.plupload.queue.css
./plupload/jquery.plupload.queue/img
./plupload/jquery.plupload.queue/img/backgrounds.gif
./plupload/jquery.plupload.queue/img/buttons-disabled.png
./plupload/jquery.plupload.queue/img/buttons.png
./plupload/jquery.plupload.queue/img/delete.gif
./plupload/jquery.plupload.queue/img/done.gif
./plupload/jquery.plupload.queue/img/error.gif
./plupload/jquery.plupload.queue/img/throbber.gif
./plupload/jquery.plupload.queue/img/transp50.png
./plupload/jquery.plupload.queue/jquery.plupload.queue.js
./plupload/jquery.ui.plupload
./plupload/jquery.ui.plupload/css
./plupload/jquery.ui.plupload/css/jquery.ui.plupload.css
./plupload/jquery.ui.plupload/img
./plupload/jquery.ui.plupload/img/plupload-bw.png
./plupload/jquery.ui.plupload/img/plupload.png
./plupload/jquery.ui.plupload/jquery.ui.plupload.js
./plupload/plupload.browserplus.js
./plupload/plupload.flash.js
./plupload/plupload.flash.swf
./plupload/plupload.full.js
./plupload/plupload.gears.js
./plupload/plupload.html4.js
./plupload/plupload.html5.js
./plupload/plupload.js
./plupload/plupload.silverlight.js
./plupload/plupload.silverlight.xap

与其将这些文件重新定位到各种样式表、javascript 和图像目录中,不如将它们保留在原处并使用 Sprockets require 指令引用它们。这是如何完成的,特别是对于图像文件和其他资源(如 .swf 和 .xap)?

The Plupload plugin is a good example. Here's the listing of the plugin added to the vendor directory:

./plupload/jquery.plupload.queue
./plupload/jquery.plupload.queue/css
./plupload/jquery.plupload.queue/css/jquery.plupload.queue.css
./plupload/jquery.plupload.queue/img
./plupload/jquery.plupload.queue/img/backgrounds.gif
./plupload/jquery.plupload.queue/img/buttons-disabled.png
./plupload/jquery.plupload.queue/img/buttons.png
./plupload/jquery.plupload.queue/img/delete.gif
./plupload/jquery.plupload.queue/img/done.gif
./plupload/jquery.plupload.queue/img/error.gif
./plupload/jquery.plupload.queue/img/throbber.gif
./plupload/jquery.plupload.queue/img/transp50.png
./plupload/jquery.plupload.queue/jquery.plupload.queue.js
./plupload/jquery.ui.plupload
./plupload/jquery.ui.plupload/css
./plupload/jquery.ui.plupload/css/jquery.ui.plupload.css
./plupload/jquery.ui.plupload/img
./plupload/jquery.ui.plupload/img/plupload-bw.png
./plupload/jquery.ui.plupload/img/plupload.png
./plupload/jquery.ui.plupload/jquery.ui.plupload.js
./plupload/plupload.browserplus.js
./plupload/plupload.flash.js
./plupload/plupload.flash.swf
./plupload/plupload.full.js
./plupload/plupload.gears.js
./plupload/plupload.html4.js
./plupload/plupload.html5.js
./plupload/plupload.js
./plupload/plupload.silverlight.js
./plupload/plupload.silverlight.xap

Instead of relocating these files into various stylesheets, javascripts, and images directories, it's better to leave them in place and reference them with the Sprockets require directive. How is this done, particularly with respect to image files and other assets like .swf and .xap?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

爱情眠于流年 2024-11-16 06:18:48

我可能是错的,但是,正如 Rails 文档中提到的:

这并不是说资产可以(或不应该)再放置在
民众;它们仍然可以并且将会由
应用程序或网络服务器。如果您愿意,您只会使用应用程序/资产
您的文件在提供之前要经过一些预处理。
http://ryanbigg.com/guides/asset_pipeline.html

因为您不需要任何预-处理这些文件,旧的公用文件夹可以是您的答案吗?

I may be wrong, but, as mentioned in Rails documentation :

This is not to say that assets can (or should) no longer be placed in
public; they still can be and will be served as static files by the
application or web server. You would only use app/assets if you wish
your files to undergo some pre-processing before they are served.
http://ryanbigg.com/guides/asset_pipeline.html

As you don't want any pre-processing on these files, could the good old public folder be your answer?

哑剧 2024-11-16 06:18:42

您可以使用 Sprockets provide 指令

例如,这就是我使用 Plupload 的方式:

# app/assets/javascripts/plupload.js
//= require plupload/plupload
//= require plupload/plupload.flash
//= require plupload/plupload.silverlight
//= provide plupload/dependencies

相应的供应商目录是这样组织的:

vendor
├── assets
│   ├── javascripts
│   │   └── plupload
│   │       ├── dependencies
│   │       │   ├── plupload.flash.swf
│   │       │   └── plupload.silverlight.xap
│   │       ├── plupload.flash.js
│   │       ├── plupload.js
│   │       └── plupload.silverlight.js
│   └── stylesheets
└── plugins

然后,当我想使用 Plupload 时,我使用 <%= javascript_include_tag 'plupload' %> ,并使用用于填充 Plupload 配置的 asset_path 帮助器:

<%= javascript_include_tag 'plupload' %>

<script type="text/javascript">
$(function() {
    var uploader = new plupload.Uploader({
        runtimes : 'flash,silverlight',
        multipart : true,
        multipart_params : {
            'authenticity_token' : '<%= form_authenticity_token %>'
        },
        flash_swf_url : 
            '<%= asset_path "plupload/dependencies/plupload.flash.swf" %>',
        silverlight_xap_url :
            '<%= asset_path "plupload/dependencies/plupload.silverlight.xap" %>',
        url : '<%= url_for [@item, :photos] %>',
        // ...
    });

希望有帮助。

You can use the Sprockets provide directive.

For example, this is how I am using Plupload:

# app/assets/javascripts/plupload.js
//= require plupload/plupload
//= require plupload/plupload.flash
//= require plupload/plupload.silverlight
//= provide plupload/dependencies

The corresponding vendor directory is organised like this:

vendor
├── assets
│   ├── javascripts
│   │   └── plupload
│   │       ├── dependencies
│   │       │   ├── plupload.flash.swf
│   │       │   └── plupload.silverlight.xap
│   │       ├── plupload.flash.js
│   │       ├── plupload.js
│   │       └── plupload.silverlight.js
│   └── stylesheets
└── plugins

I then use <%= javascript_include_tag 'plupload' %> when I want to use Plupload, and use the asset_path helper to populate the Plupload configuration:

<%= javascript_include_tag 'plupload' %>

<script type="text/javascript">
$(function() {
    var uploader = new plupload.Uploader({
        runtimes : 'flash,silverlight',
        multipart : true,
        multipart_params : {
            'authenticity_token' : '<%= form_authenticity_token %>'
        },
        flash_swf_url : 
            '<%= asset_path "plupload/dependencies/plupload.flash.swf" %>',
        silverlight_xap_url :
            '<%= asset_path "plupload/dependencies/plupload.silverlight.xap" %>',
        url : '<%= url_for [@item, :photos] %>',
        // ...
    });

Hope that helps.

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