【问题标题】:Status of Rails' link_to_function deprecation?Rails 的 link_to_function 弃用状态?
【发布时间】:2013-01-14 19:08:48
【问题描述】:

Rails 中link_to_function Javascript 助手的状态如何?我读到,包括在 this stackoverflow question 中,它在 Rails 3.0 中被弃用,然后被弃用,然后在 3.2.4 中再次被弃用。它是我可以依靠并教给学生的东西吗?我刚看了release notes (from a search) for Rails 3.2.8

恢复了 button_to_function 和 link_to_function 助手的弃用。拉斐尔·门东萨·弗兰萨

它现在处于什么位置?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 ruby-on-rails-4 link-to-function


    【解决方案1】:

    link_to_function NOT3-2-stable 分支中被弃用,并且将来不会在 3-2-stable 中被弃用。但是它IS在当前的 master 分支中被弃用,并且在 Rails 4.0 发布时将被弃用。所以我猜它会从 4.1 的 rails 代码中删除。 所以你可以教学生这样做(来自 rails 4 更新日志):

    我们建议改用不显眼的 JavaScript。例如:

    link_to "Greeting", "#", class: "nav_link"
    
    $(function() {
      $('.nav_link').click(function() {
        // Some complex code
    
        return false;
      });
    });
    

    link_to "Greeting", '#', onclick: "alert('Hello world!'); return false", class: "nav_link"
    

    【讨论】:

    • 在 helper 中使用 link_to_function 时,这个重构会是什么样子?
    • @Btuman 你找到解决方案了吗?我升级了 Rails 并且不推荐使用 link_to_function,我不确定如何为它做不显眼的 js。
    • 我想我做到了,唉,我不再有权访问代码。基本上不得不从头开始重做。 (这个辅助函数的贬值让我抓狂)
    • 这会在 javascript 之后重新加载页面。添加 remote: true 会停止客户端刷新,但它仍会向服务器发送请求.. 有没有办法一起停止请求?
    【解决方案2】:

    这是我对这个问题的解决方案:

    在javascript中:

    // define function to be called
    function awesome_func(a,b,c){
      console.log(a,b,c);
    }
    
    //clean implementation of link_to_function
    $(function(){
      $('[data-on][data-call][data-args]').each(function(d){
        try{
           $(this).on( $(this).data('on'), function(){
              window[$(this).data('call')].apply(window,$(this).data('args'))})
        }catch(e){
           if(typeof(console) != 'undefined' && typeof(console.log === 'function'))
             console.log(e);
        }
      });
    })
    

    然后你可以在rails中做:

    link_to 'Awesome Button', '#', data:{on: :click, call: 'awesome_func',args: '[1,"yeah",{b:4}]'
    

    这似乎是他们希望我们编码的方式 :),不过我喜欢 link_to_function

    【讨论】:

    • 优秀的解决方案,应该是(新的)接受的答案 IMO。注意第一个函数可以在页面上,第二个函数需要在jquery加载后调用,所以在布局上,
    • 您必须注意一些安全问题:如果您有某种所见即所得(tinymce 或 fckeditor),则需要始终删除用户上的 data-on、data-click 和 data-args提供 div 元素。
    【解决方案3】:

    以 Elias Baixas 的回答为基础...如果这对任何人都有帮助,我必须对其进行一些更改才能使其正常工作...我必须添加 evalpreventDefault(我在 JS fwiw 上非常糟糕)

    link_to fa_icon('info-circle'),
            '#',
            data: {
              on: :click,
              call: 'channel_info',
              args: Array('some data').to_json
            }
    
    
    function channel_info(a){
      console.log(a)
    }
    
    //clean implementation of link_to_function
    $(function(){
      $('[data-on][data-call][data-args]').each(function(d){
        try{
          $(this).on( $(this).data('on'), function(event){
            console.log($(this).data('args'));
            window[$(this).data('call')].apply(window,eval($(this).data('args')));
            event.preventDefault();
          })
        } catch(e) {
         if(typeof(console) != 'undefined' && typeof(console.log === 'function'))
           console.log(e);
        }
      });
    })
    

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      相关资源
      最近更新 更多