renderページjavascript呼び出し


シナリオ:

    rail           auto_complete_field ,          javascript  ,        。       javascript_function       。
a.rhtml   render _b.rhtml   。  _b.rhtml       javascript  ,    render a.rhtml 。

  _b.rhtml   auto_complete_field          。  auto_complete_result       javascript   ,    javascript      。

解決方法:

 application_helper.rb      :   javascript  ,     _b.rhtml   javascript_function           。

  def auto_complete_field_no_javascript_tag(field_id, options = {})
    function =  "var #{field_id}_auto_completer = new Ajax.Autocompleter("
    function << "'#{field_id}', "
    function << "'" + (options[:update] || "#{field_id}_auto_complete") + "', "
    function << "'#{url_for(options[:url])}'"

    js_options = {}
    js_options[:tokens] = array_or_string_for_javascript(options[:tokens]) if options[:tokens]
    js_options[:callback]   = "function(element, value) { return #{options[:with]} }" if options[:with]
    js_options[:indicator]  = "'#{options[:indicator]}'" if options[:indicator]
    js_options[:select]     = "'#{options[:select]}'" if options[:select]
    js_options[:paramName]  = "'#{options[:param_name]}'" if options[:param_name]
    js_options[:frequency]  = "#{options[:frequency]}" if options[:frequency]

    { :after_update_element => :afterUpdateElement, 
      :on_show => :onShow, :on_hide => :onHide, :min_chars => :minChars }.each do |k,v|
      js_options[v] = options[k] if options[k]
  end

    function << (', ' + options_for_javascript(js_options) + ')')

    function
  end