2012-04-13 69 views
3

在顯示頁面中,我會顯示定期事件發生日期的列表,並計算所有日期的統計數據,如最大值,最小值和連續日期之間的平均間隔。如何在完成best_in_place編輯時觸發事件?

我使用best_in_place gem來允許就地編輯日期。但是,每次更改日期時,統計信息都必須從服務器計算並重新呈現。

如何掛鉤回調函數以完成best_in_place編輯,以便統計信息可以重新呈現?

這是show.html.erb

<td id="event_date"> 
    <%= best_in_place @event, :occur_date %> 
</td> 

我的Rails代碼的HTML是

<td id="event_date"> 
    <span class='best_in_place' id='best_in_place_event_132_occur_date' data-url='/events/132' data-object='event' data-attribute='occur_date' data-type='input'>2012-03-23</span> 
</td> 

我嘗試下面的咖啡腳本代碼:

jQuery -> 
$("#best_in_place_event_*_occur_date").live 'ajax:complete', (evt, data, status, xhr) -> 
    alert "a date has changed" 

這並未我似乎沒有工作,編輯日期(發生日期)後沒有任何反應。

任何人都知道我應該在成功的best_in_place編輯時觸發事件嗎?

+0

你說,「統計數據必須進行計算,並從服務器重新呈現。」所以這個重新計算在模型中發生,並在更新操作中從控制器調用,對吧?只是在回答之前試圖弄清楚我的事實。 – IAmNaN 2012-04-14 21:23:24

+0

是的,統計計算是在模型中完成的,並且統計數據(例如max,min,avg)不存儲在ActiveRecord中。不知道我是否做得對,但我沒有通過控制器,我有視圖調用模型方法。這是我的代碼。在'views/events/show.html.erb'中\t \t \t <%= render:partial =>「events_stats」,:locals => {:event => @event}%> 而在'views/events/_event_stats .html.erb': <%stats = event。get_event_stats%> <% if stats%>最短:<(%)=統計[:分鐘]%>天最長:<(%)=統計[:最大]%>天平均:<(%)=統計[:平均]%>天 <% end %> – 2012-04-17 04:36:03

回答

2

那麼,看看源代碼,它似乎像best_in_place正確地觸發它的loadSuccessCallback中的ajax:success事件。這意味着你應該能夠使用一些jQuery將你自己的函數綁定到已更改的事件上。

$(".best_in_place").live 'change', (event) -> 
    alert "Hello, World!" 
+2

這很好。只要確保用新的「on」方法替換jquery的「live」方法。 – Stone 2013-09-14 02:12:20

+1

對,@Stone。 Karen在她的問題中使用了「live」,她並沒有顯示出我可以用'on'來使用的父元素,而且這也是一箇舊的答案。 – IAmNaN 2013-09-17 01:14:37

+0

完全,只是想把那裏告訴任何人看現在的答案。感謝您的答案btw! – Stone 2013-09-18 00:33:04

1

爲您例如:

<td id="event_date"> 
    <%= best_in_place @event, :occur_date, :classes => 'bip_date' %> 
</td> 

$('.bip_date').bind("ajax:success", function(){ 
alert("a date has changed") 
}); 
// or: $('.event_data .best_in_place').bind(...)  

而且從official readme

的 'AJAX:成功' 事件被成功時觸發。

使用綁定:

$('.best_in_place').bind("ajax:success", function() 
{$(this).closest('tr').effect('highlight'); }); 

要綁定一個回調是針對某一特定領域,使用 「類」選項輔助方法,然後綁定到這個類。

<%= best_in_place @user, :name, :classes => 'highlight_on_success' %> 
<%= best_in_place @user, :mail, :classes => 'bounce_on_success' %> 

$('.highlight_on_success').bind("ajax:success", function(){bip_date}); 
$('.bounce_on_success').bind("ajax:success", function(){$(this).closest('tr').effect('bounce'));}); 
相關問題