Can I use CoffeeScript in the views executed on render.js?

后端 未结 4 1453
长发绾君心
长发绾君心 2021-02-07 20:18

What do I need to do so that I can use CoffeeScript in the Rails JS views? For example:

def index
    format.js { render :layout => false } 
end
相关标签:
4条回答
  • 2021-02-07 20:58

    Johnny's answer is correct. If you look at the pull request linked to from the CoffeeBeans page, you have dhh saying

    Once we have a fast, clean implementation, it's welcome in core. 3.2 is a more likely target, though.

    I briefly talked with Sam Stephenson and Josh Peek about this at Railsconf, since this was a missing feature people had asked me about after my CoffeeScript talk. After all, Rails 3.1 is pushing CoffeeScript as a default pretty hard; it seems odd that there are places where pure JS has to be used. Sam's reaction was that this wouldn't be efficient, because you'd have to fire up the CoffeeScript compiler on every page request, even in production. That's because code like

    <%= coffee_script_tag do %>
      alert "coffee script is #{verb}!"
    <% end %>
    

    creates an ERB interpolation (not a CoffeeScript interpolation—unfortunate that both use the same syntax), potentially yielding a different string of CoffeeScript code on every request. And there's no way to tell, from the coffee_script_tag implementation, whether the given code is going to be the same every time (i.e. whether there's an ERB interpolation or not).

    Now, the CoffeeScript compiler is very fast, but compiling to JavaScript is still going to add a little extra time to each request. So the Rails team is hesitant to encourage the practice.

    For the sake of efficiency, and to avoid the ambiguity between ERB interpolations and CoffeeScript interpolations, you should probably keep your CoffeeScript somewhere (perhaps as a .coffee file in the same directory as your view) and compile it to JavaScript by hand.

    0 讨论(0)
  • 2021-02-07 21:08

    It's not yet supported in 3.1. You will need to use Coffeebeans.


    Update: It is now supported in rails 3.2

    0 讨论(0)
  • 2021-02-07 21:16

    This is now working in Rails 3.2. For example, I have a resource named book. This resource has a file at app/views/books/index.html.erb with the following:

    <%= link_to 'test me', new_book_path(color: 'blue'), remote: true %>
    

    Then I have a file at app/views/books/new.js.coffee at ≈ with the following code:

    test = ->
      'this is a test'
    
    console.log test()
    console.log( "<%= params[:color] %>" )
    

    I see:

    'this is a test'
    'blue'
    

    in my browser console.

    0 讨论(0)
  • 2021-02-07 21:19

    if you dont want to install coffeebeans, heres a sort of quick and dirty way to do it by hacking into the erb outputter a bit :

    <% 
    require 'coffee-script'; 
    def coffee_script; CoffeeScript.compile yield '' end %>
    <script type="text/javascript">
    <% compiled = coffee_script do |_erbout|%>
    ->
      console.log 'works! this part is coffeescript!'
    <% end %>
    <% _erbout.concat compiled %>
    </script>
    
    0 讨论(0)
提交回复
热议问题