Skip to content

Commit

Permalink
Changes APIs to fix active users. Listen to unsubscribe instead of di…
Browse files Browse the repository at this point in the history
…sconnect event. Changes user list to new APIs

Signed-off-by: Akash Manohar J <[email protected]>
  • Loading branch information
HashNuke committed Mar 14, 2012
1 parent a68466e commit cbb4f4f
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Kandan.Collections.ActiveUsers extends Backbone.Collection
url: "active_users"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 2 additions & 2 deletions app/assets/javascripts/backbone/data/active_users.js.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Kandan.Data.ActiveUsers
@callbacks: []
@callbacks: {"change": [] }

@all: ()->
Kandan.Helpers.ActiveUsers.all()

@register_callback: (event, callback)->
@callbacks.push(callback)
@callbacks[event].push(callback)

@unregister_callback: (event, callback)->
delete @callbacks[@callbacks.indexOf(callback)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Kandan.Helpers.ActiveUsers

@all: ()->
# TODO return the active users list
@all: (options)->
$(document).data("active_users")
15 changes: 14 additions & 1 deletion app/assets/javascripts/backbone/kandan.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,20 @@ window.Kandan =
for channel in channels.models
window.broadcaster.subscribe "/channels/#{channel.get('id')}"

console.log "plugins", Kandan.Plugins.all()
$(document).bind 'changeData', (element, name, value)->
if(name=="active_users")
callback() for callback in Kandan.Data.ActiveUsers.callbacks.change

active_users = new Kandan.Collections.ActiveUsers()
active_users.fetch({
success: ()->
# TODO fix because the current user doesnt get the first event
active_users.add([$(document).data('current_user')])
$(document).data("active_users", active_users.toJSON())
Kandan.Plugins.init_all()
Kandan.Widgets.init_all()
})

})


Expand Down
8 changes: 8 additions & 0 deletions app/assets/javascripts/backbone/modifiers.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Kandan.Modifiers
@modifiers: []

@register: (regex, callback)->
@modifiers.push({regex: regex, callback: callback})

@all: ()->
@modifiers
5 changes: 4 additions & 1 deletion app/assets/javascripts/backbone/plugins.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ class Kandan.Plugins
@plugins.push(plugin)

@all: ()->
@plugins
@plugins

@init_all: ()->
eval(plugin).init() for plugin in @all()
26 changes: 12 additions & 14 deletions app/assets/javascripts/backbone/plugins/user_list.js.coffee
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
class Kandan.Plugins.UserList
@active_users: ()->
console.log "active users", $(document).data("active_users")
$(document).data("active_users") || []

@widget_name: "user_list"

@render: ()->
users = []
for user in @active_users()
users.push user.first_name
$(".user_list").html(users.join ", ")
@render: ($el)->
console.log "rendering user list"
$users = $("<ul></ul>")

@init: ()->
console.log "user list plugin started"
$('body').prepend($("<div class='user_list'></div>"))
@render()
$(document).bind 'changeData', (event, name, value)=>
@render() if name == "active_users"
for user in Kandan.Data.ActiveUsers.all()
$users.append "<li>#{user.first_name} #{user.last_name}</li>"
$el.html($users)

@init: ()->
console.log "init user plugin"
Kandan.Widgets.register(@widget_name, "Kandan.Plugins.UserList")
Kandan.Data.ActiveUsers.register_callback "change", ()=>
Kandan.Widgets.render(@widget_name)

Kandan.Plugins.register "Kandan.Plugins.UserList"
26 changes: 26 additions & 0 deletions app/assets/javascripts/backbone/widgets.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Kandan.Widgets
@widgets: {}

@register: (widget_name, callback)->
@widgets[widget_name] = callback

@all: ()->
@widgets

@widget_names: ()->
widget_names = []
for widget_name of @all()
widget_names.push(widget_name) if @all().hasOwnProperty(widget_name)
widget_names

@init_all: ()->
console.log "widget_names", @widget_names()
@init(widget_name) for widget_name in @widget_names()

@init: (widget_name)->
$(".sidebar").append("<div class='#{widget_name}'></div>")
@render(widget_name)

@render: (widget_name)->
$widget_el = $(".sidebar .#{widget_name}")
eval(@widgets[widget_name]).render($widget_el) if $widget_el != []
7 changes: 7 additions & 0 deletions app/controllers/apis_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ApisController < ApplicationController
def active_users
respond_to do |format|
format.js { render :json => ActiveUsers.all }
end
end
end
13 changes: 3 additions & 10 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
<%- if user_signed_in? %>
<%- current_user_data = {
:id => current_user.id,
:name => "#{current_user.first_name} #{current_user.last_name}",
:first_name => current_user.first_name,
:last_name => current_user.last_name,
:auth_token => current_user.authentication_token
}
%>
Expand All @@ -23,17 +24,9 @@
<%- end %>

<%= javascript_tag do %>

$(document).data('active_users', [])
<%- end %>

<%= javascript_tag do %>
$(document).ready(function(){
$.each(Kandan.Plugins.all(), function(index, plugin){
eval(plugin).init()
})
})
<%- end %>

<%= csrf_meta_tags %>
</head>
<body>
Expand Down
2 changes: 2 additions & 0 deletions app/views/main/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
<%- end %>

</div>
<div class="sidebar">
</div>
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ faye_server.add_extension(DeviseAuth.new)

FAYE_CLIENT = faye_server.get_client

faye_server.bind(:disconnect) do |client_id|
faye_server.bind(:unsubscribe) do |client_id|
ActiveUsers.remove_by_client_id(client_id)
end

Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
resources :activities
end

get "/active_users" => "apis#active_users"

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down

0 comments on commit cbb4f4f

Please sign in to comment.