Ajaxify the mark read/unread function for messages so it doesn't

reload the whole page each time.
This commit is contained in:
Tom Hughes 2010-02-26 16:35:36 +00:00
parent 30c47f4ac4
commit 3851a672e2
4 changed files with 14 additions and 6 deletions

View file

@ -100,10 +100,17 @@ class MessageController < ApplicationController
end end
message.message_read = message_read message.message_read = message_read
if message.save if message.save
if request.xhr?
render :update do |page|
page.replace "inbox-count", :partial => "message_count"
page.replace "inbox-#{message.id}", :partial => "message_summary", :object => message
end
else
flash[:notice] = notice flash[:notice] = notice
redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
end end
end end
end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
@title = t'message.no_such_user.title' @title = t'message.no_such_user.title'
render :action => 'no_such_user', :status => :not_found render :action => 'no_such_user', :status => :not_found

View file

@ -0,0 +1 @@
<p id="inbox-count"><%= t'message.inbox.you_have', :new_count => @user.new_messages.size, :old_count => (@user.messages.size - @user.new_messages.size) %></p>

View file

@ -1,13 +1,13 @@
<% this_colour = cycle('lightgrey', 'white') %> <% this_colour = cycle('lightgrey', 'white') %>
<tr class="inbox-row<%= "-unread" if not message_summary.message_read? %>"> <tr id="inbox-<%= message_summary.id %>" class="inbox-row<%= "-unread" if not message_summary.message_read? %>">
<td class="inbox-sender" bgcolor="<%= this_colour %>"><%= link_to h(message_summary.sender.display_name), :controller => 'user', :action => message_summary.sender.display_name %></td> <td class="inbox-sender" bgcolor="<%= this_colour %>"><%= link_to h(message_summary.sender.display_name), :controller => 'user', :action => message_summary.sender.display_name %></td>
<td class="inbox-subject" bgcolor="<%= this_colour %>"><%= link_to h(message_summary.title), :controller => 'message', :action => 'read', :message_id => message_summary.id %></td> <td class="inbox-subject" bgcolor="<%= this_colour %>"><%= link_to h(message_summary.title), :controller => 'message', :action => 'read', :message_id => message_summary.id %></td>
<td class="inbox-sent nowrap" bgcolor="<%= this_colour %>"><%= l message_summary.sent_on %></td> <td class="inbox-sent nowrap" bgcolor="<%= this_colour %>"><%= l message_summary.sent_on %></td>
<% if message_summary.message_read? %> <% if message_summary.message_read? %>
<td><%= button_to t('message.message_summary.unread_button'), :controller => 'message', :action => 'mark', :message_id => message_summary.id, :mark => 'unread' %></td> <td><%= button_to t('message.message_summary.unread_button'), {:controller => 'message', :action => 'mark', :message_id => message_summary.id, :mark => 'unread'}, { :onclick => remote_function(:url => {:controller => 'message', :action => 'mark', :message_id => message_summary.id, :mark => 'unread'}) + "; return false;" } %></td>
<% else %> <% else %>
<td><%= button_to t('message.message_summary.read_button'), :controller => 'message', :action => 'mark', :message_id => message_summary.id, :mark => 'read' %></td> <td><%= button_to t('message.message_summary.read_button'), {:controller => 'message', :action => 'mark', :message_id => message_summary.id, :mark => 'read'}, { :onclick => remote_function(:url => {:controller => 'message', :action => 'mark', :message_id => message_summary.id, :mark => 'read'}) + "; return false;" } %></td>
<% end %> <% end %>
<td><%= button_to t('message.message_summary.reply_button'), :controller => 'message', :action => 'reply', :message_id => message_summary.id %></td> <td><%= button_to t('message.message_summary.reply_button'), :controller => 'message', :action => 'reply', :message_id => message_summary.id %></td>
<td><%= button_to t('message.message_summary.delete_button'), :controller => 'message', :action => 'delete', :message_id => message_summary.id, :referer => request.request_uri %></td> <td><%= button_to t('message.message_summary.delete_button'), :controller => 'message', :action => 'delete', :message_id => message_summary.id, :referer => request.request_uri %></td>

View file

@ -1,6 +1,6 @@
<h2><%= t'message.inbox.my_inbox'%>/<%= link_to t('message.inbox.outbox'), url_for(:controller => "user", :action => "outbox", :id => @user.display_name) %></h2> <h2><%= t'message.inbox.my_inbox'%>/<%= link_to t('message.inbox.outbox'), url_for(:controller => "user", :action => "outbox", :id => @user.display_name) %></h2>
<p><%= t'message.inbox.you_have', :new_count => @user.new_messages.size, :old_count => (@user.messages.size - @user.new_messages.size) %></p> <%= render :partial => "message_count" %>
<% if @user.messages.size > 0 %> <% if @user.messages.size > 0 %>
<div id="messages"> <div id="messages">