views:

28

answers:

0

Hi, I am new to both Rails and Javascript, so my question is probably trivial.

I am currently building a website which have a side page for index page. Inside the side panel, there is a search field for the index page, which I used for autocomplete ( Ajax.Autocompleter from the script.aculo.us). The problem is that seems autocomplete javascript would stop working after a page refresh for the side panel page. How could I get around this?

Code for the view part

= content_for(:javascript_epilogue) do
:plain
  document.observe("dom:loaded", function() {
  issues.account_suggest("#{current_auto_complete}");
});

And Code for the javascript function it self

//Auto populate list of accounts based on the textfiled user input
  account_suggest: function(controller, focus) {
    this.autocompleter = new Ajax.Autocompleter("account_suggest_query", "account_complete_dropdown", "/" + controller + "/account_complete", { 
      frequency: 0.25,
      afterUpdateElement: function(text, el) {
        if (el.id) {  // found: redirect to #show
        this.search_account(text.value, controller)
        } else {      // not found: refresh current page
            var patt=/^\s$/;
            var test_result = patt.test(text.value);
            if (test_result) {
            //alert("Empty char \"" +text.value +  "\"");
            $("account_suggest_query").clear();
            }
          this.search_account("", controller)
          }
      }.bind(this)    // binding for this.base_url
    });
    $("account_complete_dropdown").update("");
    $("account_suggest_query").value = "";
    if (focus) {
      $("account_suggest_query").focus();
    }
  },

I think the problem could be that the javascript only execute once when the page first loaded, how could I get around this? I did try call the javascript function from the partial page where the autocomplete input text_field page but it doesn't work.

So my question could be simplified as: 1.Is my problem because of the javascript for Autocomplete only execute once page loaded? 2. Or something wrong with my javascript function for autocomplete?

Cheers