我有一个应用程序,它使用一些 Javascript 来处理基本的 Ajax 请求,例如自动完成和实时搜索。例如,我通过以下方式实现了实时搜索;我发现了一些潜在的问题,想和你谈谈,以便有更好的代码。
应用程序/控制器/company_controller.rb
def livesearch
  @companies = Company.search(params[:query])
  render :partial => "companies", :locals => {:companies => @companies}
end
应用程序/视图/公司/_companies.html.haml
- if companies.empty?
  None
- else
  %table#company_list
    %tr
      %th Name
      %th Description
      %th Products
      %th
    = render 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"
    });
公共/javascripts/companies.js
function update_listed_companies(query) {
  if(typeof query == "undefined")
    query = "";
  $("#company_list_container").showWith(
    "/companies/livesearch?query=" + query,
    false
  );
}
公共/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);
以下是让我怀疑我的代码的最优性的事情:
- 我有 Javascript 代码
_livesearch_box.html.haml。 - 即使我把它放在里面,
public/javascripts/companies_livesearch.js我也必须对其中的#search部分进行硬编码。 - 我有
#company_list_container(这_companies.html.haml是呈现的 div)硬编码在public/javascripts/companies.js. - 我将路径
/companies/liveseach?query=硬编码在public/javascript/companies.js. - 我没有使用 CoffeeScript,主要是因为它期望(至少如果您使用 Barista)在某处(例如 in 
app/coffeescripts/)找到纯 JavaScript 代码并将其编译为public/javascripts. 但在我的应用程序中,我的;中也有一些.js.erb文件。app/views/companies例如,我有一个投票系统,它在 app/views/companies/_vote.js.erb中使用以下内容:$("#vote_link_<%= escape_javascript(@company.id.to_s) %>").html("<%= escape_javascript(vote_link_for(@company)) %>")将“投票这家公司”链接替换为“取消投票这家公司”链接(反之亦然) Ajax 请求并由控制器中的voteand操作呈现。unvote我知道有 coffee-haml-filter 可以在 haml 文件中编译 CoffeeScript,但这不是我真正需要的,通常被弃用并被视为脏东西(?)。 
所以问题至少是:
- 如何在我的
app/views/*/*.js.*? - 我应该有
app/views/*/*.js.*文件吗? - 如何以最有效和最优雅的方式删除所有这些元素 id 和那些在 javascripts 中硬编码的路径?
 
很抱歉这个问题很长,感谢您结束它!