2010-11-12 21 views
0

我試圖完成在軌3的observe_field在Rails 3中

在導軌2.3.5的的observe_field我: -

<%= observe_field 'query', :frequency => 2, 
          :update => "search_results",         
          :url => {:controller => params[:area], :action => "search",:area => params[:area]}, 
          :method=>:get, 
          :with => "query" %> 

工作正常,檢查我的text_field_tag 「查詢」 和更新我的「搜索結果」每兩秒鐘。這就是我試圖用原型來模擬的東西。

目前我有一個基本的應用程序中的Rails 3: -

<script> 
document.observe("dom:loaded", function() { 
$('search').observe('change', 
respondToChange()); 
}); 
</script> 

<script> 
document.observe("dom:loaded", function() { 
new Form.Element.Observer(
'search', 
1, 
respondToChange() 
) }); 
</script> 

這兩者觸發respondToChange功能在頁面加載時,儘管「DOM:加載「,然後不再觀察。

有沒有人有任何想法,我可能會獲得我的「搜索」text_field_tag重複的觀察員檢查。

+2

男人,我建議你在問題格式上多做一點工作,這有點麻煩。 – 2010-11-12 02:30:21

+0

謝謝我只是學習如何和我希望我已經正確編輯它 – MDM 2010-11-14 01:43:46

回答

0

與感謝「themiddleman」,並在以太「agnaki」的地方在那裏,我已經解決了這個問題:

使用respondToChange,不respondToChange()

由於括號()執行的功能,而沒有()它引用它。

$('search').observe('change', respondToChange); 
// only triggers when the focus is moved away from the text_field. 

new Form.Element.Observer(
    'search', 
    1, 
respondToChange 
) }); 

//................................repeatedly checks the text_field,every 1 second, and call the function if there is a any change. 
+0

我已經破解它。 $( '搜索')。向上( '形式')。提交() – MDM 2010-11-14 01:20:24

0

這是我的完整代碼,如果任何人有興趣。該代碼每2秒觀察一次text_field_tag「搜索」,如果值發生變化,它會自動觸發搜索。我認爲,提交按鈕現在可以取消。我可能會添加:autocomplete => "off", :onKeyPress=>"return disableEnterKey(event)") %>到text_field_tag以禁用返回鍵,不確定。

在我index.html.erb我:

<h1>Listing homepages</h1> 
<div id = "testsearch"> 
    <%=render :partial => 'homepage'%>  
</div> 
<%= form_tag homepages_path, :method => 'get', :remote => true do %> 
<%= label_tag(:search, "Search for:") %> 
<%= text_field_tag :search, params[:search]%> 
<%= submit_tag "search", :name => nil %> 
<%end%> 

<%= set_focus_to_id 'search' %>    // I have a helper "set_focus_to_id" 

<script> 
document.observe("dom:loaded", function() { // ensures the page is loaded first 
new Form.Element.Observer(     // Observes the text_field_tag every 2 seconds 
    'search', 
    2, 
    respondToChange       //refrences the function in the Layout <head> 
)           // on a change in search calls respondToChange 
}); 
</script> 
<br /> 
<%= link_to 'New Homepage', new_homepage_path %> 

在我的應用程序佈局的頭,我有:

<script> 
function respondToChange() { 
$('search').up('form').submit()   // The ".up finds the form in the DOM" 
}; 
</script 

在我的控制器#指數我有:

def index 
    @homepages = Homepage.search(params[:search]) //".search method is in the Model" 
    respond_to do |format| 
    format.html # index.html.erb 
    format.xml { render :xml => @homepages } 
    format.js 
end 
end 

在我的模型中我有:

def self.search(search_item) 
    if search_item 
    self.where('section LIKE ?', "%#{search_item}%") //Handles the ajax call. 
    else 
    self.all           //Handles the html call on startup. 
    end 
end 

在幫助我:

def set_focus_to_id(id) 
    javascript_tag("$('#{id}').focus()"); 
end 

在 「_homepage」 偏我有:

<table> 
    <tr> 
    <th>Id</th> 
    <th>Section</th> 
    <th>Link</th> 
    <th>Description</th> 
    <th></th> 
    <th></th> 
    <th></th> 
</tr> 

<% for homepage in @homepages %> 

    <tr> 
    <td><%= homepage.id %></td> 
    <td><%= homepage.section %></td> 
    <td><%= homepage.link %></td> 
    <td><%= homepage.description %></td> 
    <td><%= link_to 'Show', homepage %></td> 
    <td><%= link_to 'Edit', edit_homepage_path(homepage) %></td> 
    <td><%= link_to 'Destroy', homepage, :confirm => 'Are you sure?', :method => :delete %></td> 
    </tr> 
<%end%> 

</table> 

而在index.js.erb的我:

$('testsearch').update("<%= escape_javascript(render :partial => 'homepage') %>"); 

如果有人對我如何改善這一點有任何意見,請與我聯繫或說出口。

0

我想我現在已經解決了通過編程來做ajax調用的問題。我將不得不在所有類型的瀏覽器中檢查它,但目前它在Firefox和Safari中運行。我現在也將content.for的javascript「document.observe(」dom:loaded「)和它調用的函數」respondToChange()「移動到應用程序頭部,其他文件保持不變。 .erb我現在有: -

<h1>Listing homepages</h1> 
<div id = "testsearch"> 
    <%=render :partial => 'homepage'%>  
</div> 
<%= form_tag homepages_path, :method => 'get', :remote => true do %> 
    <%= label_tag(:search, "Search for:") %> 
<%= text_field_tag :search, params[:search]%> 
<%= submit_tag "search", :name => nil %> 
<%end%> 

<%= set_focus_to_id 'search' %>    

<% content_for :search_javascript do %> 

function respondToChange() { 

    var pars = 'search=' + $('search').getValue() 

     new Ajax.Request("<%= homepages_path %>" ,{ 
     method: 'get', 
     parameters: pars 
    }); 
}; 

document.observe("dom:loaded", function() { 
    new Form.Element.Observer(    
     'search', 
     2, 
     respondToChange 

    )           
}); 

<% end %> 

<br /> 
<%= link_to 'New Homepage', new_homepage_path %> 

在我的應用程序佈局文件我現在有: - GardenR3 <%= stylesheet_link_tag:所有%> <%= javascript_include_tag:默認%> <%= csrf_meta_tag%>

<script> 
<%= yield :search_javascript %> 
</script> 

</head> 
<body> 

<%= yield %> 

</body> 
</html>