Status of Rails' link_to_function deprecation?

后端 未结 3 1202
南旧
南旧 2020-12-09 09:25

What is the status of the link_to_function Javascript helper in Rails? I read, including in this stackoverflow question, that it was deprecated in Rails 3.0, th

3条回答
  •  温柔的废话
    2020-12-09 10:01

    Building on Elias Baixas answer... I had to change it a bit to get it to work if this helps anyone... I had to add eval and preventDefault (I'm pretty terrible at 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);
        }
      });
    })
    

提交回复
热议问题