Skip to content

Commit

Permalink
added support for jquery
Browse files Browse the repository at this point in the history
Signed-off-by: David A. Cuadrado <krawek@gmail.com>
  • Loading branch information
dcu committed Apr 22, 2009
1 parent 97ca989 commit 4dd6c90
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions lib/orbited_helper.rb
@@ -1,7 +1,7 @@
# A few view helpers to generate the necessary javascript to get
# connected to the Orbited server
module OrbitedHelper

# Includes the necessary javascript files from the Orbited server (Orbited.js and stomp.js)
# This should be called after prototype.js is included
def orbited_javascript
Expand All @@ -10,50 +10,76 @@ def orbited_javascript
js += javascript_include_tag protocol_js
js
end

# Connects to the the STOMP server and subscribes to each channel in channels
# [:js]
# The JS framework that you are using, only :prototype and :jquery are supported. (string)
# [:callbacks]
# Callbacks to pass to the stomp object (Hash). ie.
# {:onmessageframe => "function(frame) {alert(frame.body)}"}
# {:onconnectedframe => "function(frame) {alert(frame.body)}"}
# [:user]
# Stomp username (string)
# [:password]
# Stomp password (string)
# [:delayed_connect]
# Assign it to true to delay the connection (Boolean)
#
def stomp_connect(channels, options = {})
callbacks = options[:callbacks] || {}
channels = [channels] unless channels.is_a? Array
subscriptions = channels.map {|channel| "stomp.subscribe('/topic/#{channel}')"}.join(';')
js = "Element.observe(window, 'load', function(){"
js += "document.domain = document.domain; "
js += "stomp = new STOMPClient(); "
js += "stomp.onmessageframe = function(frame) {eval(frame.body)}; " unless callbacks[:onmessageframe]
js += "stomp.onconnectedframe = function(frame) {#{subscriptions}}; " unless callbacks[:onconnectedframe]
js = ""
js << case options[:js].to_s
when "jquery"
"$(document).ready(function() {"
else
"Element.observe(window, 'load', function(){"
end

js << "document.domain = document.domain; "
js << "stomp = new STOMPClient(); "
js << "stomp.onmessageframe = function(frame) {eval(frame.body)}; " unless callbacks[:onmessageframe]
js << "stomp.onconnectedframe = function(frame) {#{subscriptions}}; " unless callbacks[:onconnectedframe]
callbacks.each do |callback, function|
js += "stomp.#{callback} = #{function}; "
js << "stomp.#{callback} = #{function}; "
end
user = options[:user] || OrbitedConfig.stomp_user || ''
password = options[:password] || OrbitedConfig.stomp_password || ''
host = OrbitedConfig.stomp_host
port = OrbitedConfig.stomp_port
stomp_connect = "stomp.connect('#{host}', #{port}, '#{user}', '#{password}'); "
if options[:delayed_connect]
js += "stomp.delayedConnect = function() {#{stomp_connect}};"
js << "stomp.delayedConnect = function() {#{stomp_connect}};"
else
js += stomp_connect
js << stomp_connect
end

js << case options[:js].to_s
when "jquery"
"$(window).bind('beforeunload', function() {stomp.reset()});"
else
"Element.observe(window, 'beforeunload', function(){stomp.reset()});"
end
js += "Element.observe(window, 'beforeunload', function(){stomp.reset()});"
js += "});"
js << "});"
javascript_tag js
end

private
def orbited_server_url
request.ssl? ?
request.ssl? ?
"https://#{OrbitedConfig.ssl_host}:#{OrbitedConfig.ssl_port}" :
"http://#{OrbitedConfig.host}:#{OrbitedConfig.port}"
end

def orbited_js
orbited_server_url + '/static/Orbited.js'
end

def protocol_js
orbited_server_url + "/static/protocols/#{OrbitedConfig.protocol}/#{OrbitedConfig.protocol}.js"
end

def initialize_js
<<-EOS
Orbited.settings.hostname = '#{request.ssl? ? OrbitedConfig.ssl_host : OrbitedConfig.host}';
Expand All @@ -63,5 +89,5 @@ def initialize_js
TCPSocket = Orbited.TCPSocket;
EOS
end

end

0 comments on commit 4dd6c90

Please sign in to comment.