Status of Rails' link_to_function deprecation?

China☆狼群 提交于 2019-11-27 02:37:19

问题


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, then undeprecated, then deprecated again in 3.2.4. Is it something I can depend on and teach students? I just read the release notes (from a search) for Rails 3.2.8:

Reverted the deprecation of button_to_function and link_to_function helpers. Rafael Mendonça França

Where does this stand now?


回答1:


link_to_function is NOT deprecated in 3-2-stable branch and it won't be deprecated in 3-2-stable in future. But it IS depreacated in current master branch and will be deprecated in Rails 4.0 when it releases. So I guess it will removed from rails code in 4.1. So you can teach students to do this (from the rails 4 changelog):

We recommend the use of Unobtrusive JavaScript instead. For example:

link_to "Greeting", "#", class: "nav_link"

$(function() {
  $('.nav_link').click(function() {
    // Some complex code

    return false;
  });
});

or

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



回答2:


This is my solution to this problem:

in 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);
    }
  });
})

Then you can do in rails:

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

this seems the way they want us to code :), i liked link_to_function, though



来源:https://stackoverflow.com/questions/14324919/status-of-rails-link-to-function-deprecation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!