Friday, October 21, 2011

make unobtrusive CoffeeScript on AWDwR 4th beta chapter15

On Agile Web Development with Rails 4th edition beta chapter15 (Internationalization), there is one more stuck point not-related with internationalization. In this chapter, JavaScript sample is not unobtrusive one. So I try to rewrite the sample with unobtrusive CoffeeScript.

application.js and application.html.erb

In this tutorial, we should use CoffeeScript only store.js.coffee. So we can omit to use another *.js.coffee files.
  1. In application.js file, delete the line //= require_tree ..
  2. In application.html.erb file, add the line <%= javascript_include_tag "store" %>.

make unobtrusive

In store.js.coffee, write the code for erase submit button and execute the action when drop down list is changed.
$ ->
  $('.locale input').hide()
........
$ ->
  $('#set_locale').change ->
    $(this).parent('form.locale').submit()
so that we just write the drop down list in layouts/application.html.erb like below:
    <%= form_tag store_path, class: 'locale' do %>
      <%= select_tag 'set_locale', options_for_select(LANGUAGES, I18n.locale.to_s) %>
      <%= submit_tag 'submit' %>
    <% end %>

No comments:

Post a Comment