From cc89cd5cda8781bf6be01fb71bb110e3057b525a Mon Sep 17 00:00:00 2001 From: maxwell Date: Tue, 22 Jun 2010 16:12:15 -0700 Subject: [PATCH] reload object before send to websocket --- app/models/post.rb | 1 + app/views/layouts/application.html.haml | 15 ++++++++++++++- app/views/posts/_post.html.haml | 3 +-- config/initializers/socket.rb | 17 +++++++++-------- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 6ea021e46..04cccc690 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -41,6 +41,7 @@ class Post protected def send_to_view + self.reload WebSocket.update_clients(self) end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index acfb64372..b0b07bd68 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -10,7 +10,7 @@ = javascript_include_tag 'jquery142' /= javascript_include_tag"http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" = javascript_include_tag 'rails' - + = javascript_include_tag 'jquery_form' - unless request.user_agent.include? "Safari" ||"Chrome" = javascript_include_tag 'FABridge' = javascript_include_tag 'swfobject' @@ -35,6 +35,19 @@ }, function(){ $(this).fadeTo(80, 1); }); + + // wait for the DOM to be loaded + // bind 'myForm' and provide a simple callback function + $('#new_status_message').ajaxForm(function() { + alert("Thank you for your comment!"); + }); + $('#new_bookmark').ajaxForm(function() { + alert("Thank you for your comment!"); + }); + + $('#new_blog').ajaxForm(function() { + alert("Thank you for your comment!"); + }); }); %body diff --git a/app/views/posts/_post.html.haml b/app/views/posts/_post.html.haml index 0ec4bc41e..9474a806f 100644 --- a/app/views/posts/_post.html.haml +++ b/app/views/posts/_post.html.haml @@ -1,5 +1,4 @@ %ul %h3= link_to post.class, object_path(post) - for field in object_fields(post) - %li= "#{field}: #{post.attributes[field]}" - + %li= "#{field}: #{post.attributes[field]}" \ No newline at end of file diff --git a/config/initializers/socket.rb b/config/initializers/socket.rb index 2770ba631..a6caf0a7d 100644 --- a/config/initializers/socket.rb +++ b/config/initializers/socket.rb @@ -8,14 +8,19 @@ module WebSocket EM.next_tick { EM.add_timer(0.1) do @channel = EM::Channel.new + @view = ActionView::Base.new(ActionController::Base.view_paths, {}) + class << @view + include ApplicationHelper + include Rails.application.routes.url_helpers + end end - EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws| + EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws| ws.onopen { sid = @channel.subscribe { |msg| ws.send msg } ws.onmessage { |msg| - @channel.push "#{msg}" + @channel.push msg } ws.onclose { @@ -27,12 +32,8 @@ module WebSocket } #this should get folded into message queue i think? def self.update_clients(object) - view = ActionView::Base.new(ActionController::Base.view_paths, {}) - class << view - include ApplicationHelper - include Rails.application.routes.url_helpers - end - n = view.render(:partial =>view.type_partial(object), :locals => {:post => object}) + + n = @view.render(:partial => @view.type_partial(object), :locals => {:post => object}) @channel.push(n) if @channel end