I have an application that uses some Javascript for basic Ajax requests such as autocomplete and direct search. For example, I implemented a live search as follows; I noticed some potential problem and would like to talk to you about this in order to have better code.
application / controllers / company_controller.rb
def livesearch @companies = Company.search(params[:query]) render :partial => "companies", :locals => {:companies => @companies} end
application / views / companies / _companies.html.haml
- if companies.empty? None - else %table
app / views / companies / _livesearch_box.html.haml
= content_for :scripts, "jlivesearch companies" = form_tag "#", :autocomplete => :off, :remote => true do %span.light Search: = text_field_tag :search :javascript $('#search').livesearch({ searchCallback: update_listed_companies, queryDelay: 200, innerText: "Search companies" });
public /JavaScripts/companies.js
function update_listed_companies(query) { if(typeof query == "undefined") query = ""; $("#company_list_container").showWith( "/companies/livesearch?query=" + query, false ); }
public /JavaScripts/application.js
(function($) { $.fn.showWith = function (what, popup) { element = this; $.get(what, function(data) { element.html(data); if(popup) element.bPopup(); }); return element; }; })(jQuery);
Here is what makes me suspicious about the optimality of my code:
- I have Javascript code in
_livesearch_box.html.haml . - Even if I put it in
public/javascripts/companies_livesearch.js , I would have to hardcode the part of #search in it. - I have a
#company_list_container (which is the div in which _companies.html.haml displayed), hard-coded in public/javascripts/companies.js . - I have a path
/companies/liveseach?query= encoded in public/javascript/companies.js . - I do not use CoffeeScript, mainly because it expects (at least if you use Barista) to find clean javascript code somewhere (e.g. in
app/coffeescripts/ ) and compile it into public/javascripts . But in my application, I also have a .js.erb file in my app/views/companies ; for example, I have a voting system that uses the following application / views / companies / _vote.js.erb : $("#vote_link_<%= escape_javascript(@company.id.to_s) %>").html("<%= escape_javascript(vote_link_for(@company)) %>") To replace the" Vote for this company "link with" Unvote this company "one (and vice versa) with an Ajax request and displayed by the vote and unvote in the controller. I know that there is a coffee-haml filter that compiles CoffeeScript inside haml files, but this is not what I definitely need, and is usually considered obsolete and considered something dirty (?).
So the questions are no less than:
- How to get CoffeeScript in my
app/views/*/*.js.* ? - Should I have
app/views/*/*.js.* Files at all? - How to remove all these element identifiers and these hardcoded paths in javascripts in the most efficient and elegant way?
Sorry for the long question and thank you for reaching the end!
javascript unobtrusive-javascript ruby-on-rails coffeescript dry
Alberto santini
source share