简体   繁体   English

如何在Rails资产管道中使用我的Jquery插件?

[英]How do I use my Jquery plugin in the Rails asset pipeline?

My company introduced a way to use social share buttons without providing tracking data to the social sites on page load: jquery.socialshareprivacy . 我的公司引入了一种使用社交共享按钮而无需在页面加载时向社交网站提供跟踪数据的方法: jquery.socialshareprivacy

How can I use it in a Rails 3.2 asset pipeline? 如何在Rails 3.2资产管道中使用它?

The plugin consists of a JS file and a directory with css and images. 该插件包含一个JS文件和一个包含CSS和图像的目录。

Rails 3.2 comes with jQuery 1.9.x and the jquery.socialshareprivacy.js uses functions, removed in 1.9 ( .live $.browser ), so you have to patch it (get patch at first gist ) Using the asset pipeline requires to use different file paths for the images, so I patched the CSS (see second gist ) and turned it into an SCSS (append .scss to the filename). Rails 3.2随附于jQuery 1.9.x,并且jquery.socialshareprivacy.js使用的功能已在1.9( .live $.browser )中删除,因此您必须对其进行修补( 首先获得修补程序)使用资产管道需要使用不同的方法图像的文件路径,因此我修补了CSS(请参阅第二个要点 )并将其转换为SCSS(将.scss追加到文件名之后)。
I basically turned three url() into image-url() . 我基本上把三个url()变成了image-url()

I put the JS in vendor/assets/javascripts and the whole CSS+images folder (socialshareprivacy) under vendor/assets/stylesheets 我将JS放在vendor/assets/javascripts ,并将整个CSS + images文件夹(socialshareprivacy)放在vendor/assets/stylesheets

When I place the share buttons on a page (see this documentation ), I set the image urls in the options: 当我将共享按钮放在页面上时(请参阅本文档 ),我在选项中设置图像网址:

<div id="socialshareprivacy"></div>
<script type="text/javascript">
    $(function(){
        $('#socialshareprivacy').socialSharePrivacy({
            'css_path': '<%= asset_path 'socialshareprivacy/socialshareprivacy.css' %>',
            services: {
                facebook: {dummy_img: '<%= asset_path 'socialshareprivacy/images/dummy_facebook.png' %>'},
                twitter: {dummy_img: '<%= asset_path 'socialshareprivacy/images/dummy_twitter.png' %>'},
                gplus: {dummy_img: '<%= asset_path 'socialshareprivacy/images/dummy_gplus.png' %>'}
            }
        });
    });
</script>

If you need it in several places, consider turning it into a partial. 如果您在多个地方需要它,请考虑将其变成局部的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM