简体   繁体   English

从Rails视图动态/在运行时设置jquery变量?

[英]Dynamically/at runtime set jquery variables from Rails view?

I'm looking at the Sharre social mediq jquery plugin http://sharrre.com/# Below is an example of how to use it. 我正在看Sharre社交mediq jquery插件http://sharrre.com/#以下是如何使用它的示例。

simple_jquery_social.js example: simple_jquery_social.js示例:

$('#sharrre').sharrre({
share: {
  googlePlus: true,
   facebook: true,
  twitter: true
 },
url: 'http://sharrre.com'

}) })

So in normal rails usage, I would add the above in my assets directory (along with the sharrre jquery lib stuff), put a 'div id=sharrre' tag in my view, and I'd be done. 因此,在正常轨道的使用,我想补充以上在我的资产目录(连同sharrre jQuery的lib中的东西),把一个“DIV ID = sharrre”标签在我看来,我会做。 But I would like to make a rails helper function to use in my .html.erb view template and dynamically decide whether or not to display google plus, for example. 但是,我想在我的.html.erb视图模板中使用Rails辅助函数,并动态决定是否显示google plus。 So I would want something like: 所以我想要类似的东西:

<%= my_helper :googlePlus=>false %>

Then my environment should know that the googlePlus variable in the jquery code above would now be false, and hence the googlePlus button would not be displayed. 然后我的环境应该知道上面的jquery代码中的googlePlus变量现在为false,因此不会显示googlePlus按钮。 **The whole point is to to control a lot more options available in the jquery plugin dynamically thru Rails. **重点是通过Rails动态控制jquery插件中可用的更多选项。 But to do that I need to be able to set jquery variables dynamically through Rails. 但是要做到这一点,我需要能够通过Rails动态设置jquery变量。 I realize the above example is trivial because I could just change the jquery variable by hand, but by doing it through Rails, I could also set the url param dynamically, which is something I want to be able to do so people can recommend particular pages. 我意识到上面的示例是微不足道的,因为我可以手动更改jquery变量,但是通过Rails进行操作,我也可以动态设置url参数,这是我希望能够做到的,以便人们可以推荐特定的页面。

What is a good mechanism for accomplishing this, or is it even possible? 有什么好的机制可以做到这一点,或者甚至有可能?

Create a helper that takes an ID, a URL and a hash of options as a param. 创建一个使用ID,URL和选项哈希作为参数的助手。

Then the helper generates a mix of JS and html, outputting both the JS code and the div that needs 然后,帮助程序生成JS和html的混合,同时输出JS代码和需要的div

def sharrre_tag(div_id, url, options = {})
  div_str = "<div id='#{div_id}'></div>".html_safe
  js_str = "<script>".html_safe
  #Add code to append specific js code and test all option existence and append time to js_str
  js_str += "</script>".html_safe
  return js_str + div_str
end

And in the view just call your helper with the right params 在视图中,只需使用正确的参数来调用您的助手

Option 2. Separate JS from HTML 选项2.将JS与HTML分开

If you want to achieve this, you could write a JS code in your separate JS file that'll take each element with a class sharrre_div . 如果要实现此目的,可以在单独的JS文件中编写JS代码,该代码将使用sharrre_div类获取每个元素。 For each one, it'll check if specific html data attributes exist : data-sharrre-googlePlus , data-sharrre-twitter , data-sharrre-url etc ... this option relies heavily on JS 对于每一个,它将检查是否存在特定的html数据属性: data-sharrre-googlePlusdata-sharrre-twitterdata-sharrre-url等...此选项在很大程度上依赖于JS

JS : Encapsulate it in a body onload event JS:将其封装在body onload事件中

var sharrre_attr_array = ['data-sharrre-googlePlus', 'data-sharrre-facebook', 'data-sharrre-twitter'];
$('.sharrre_div').each(function(index){
    var share_hash = {};
        var current_div = $(this);
        $.each(sharrre_attr_array, function(index, element){

          if(current_div.attr(element)){
                share_hash[element.split('data-sharrre-')[1]] = true;
            }
      });
    $(this).sharrre({
          share: share_hash
            ,url: $(this).attr('data-sharrre-url')
        });

});

All your helper has to do is then create a div like this : 您的助手所需要做的就是创建一个像这样的div:

<div class='sharrre_div' data-sharrre-googlePlus='1' data-sharrre-twitter='1' data-sharrre-url='<%=request.url%>' data-sharrre-facebook='1'></div>

Shouldn't be too hard to write :) 应该不太难写:)

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

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