Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working chat app with jQuery tabs and Backbone. Adds a small chat pos…
…ting box Signed-off-by: Akash Manohar J <akash@akash.im>
- Loading branch information
Showing
11 changed files
with
115 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ | |
//= require lib/backbone | ||
//= require backbone/kandan | ||
//= require_tree . | ||
|
18 changes: 18 additions & 0 deletions
18
app/assets/javascripts/backbone/broadcasters/faye.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Kandan.Broadcasters.FayeBroadcaster | ||
|
||
constructor: ()-> | ||
@faye_client = new Faye.Client("/remote/faye") | ||
@faye_client.disable('websocket') | ||
auth_extension = { | ||
outgoing: (message, callback)-> | ||
if message.channel == "/meta/subscribe" | ||
# TODO move fetching auth token to a helper | ||
message['ext'] = {auth_token: $.data(document, 'current_user').auth_token} | ||
callback(message) | ||
} | ||
@faye_client.addExtension(auth_extension) | ||
|
||
subscribe: (channel)-> | ||
console.log "Subscribing to #{channel}" | ||
@faye_client.subscribe channel, (data)=> | ||
console.log data.user.first_name, "says", data.content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Kandan.Views.Chatbox extends Backbone.View | ||
|
||
template: JST['chatbox'] | ||
tagName: 'div' | ||
className: 'chatbox-area' | ||
|
||
events: | ||
"keypress .chatbox": 'postMessage' | ||
|
||
postMessage: (event)-> | ||
|
||
if event.keyCode== 13 | ||
$chatbox = $(".chatbox") | ||
activity = new Kandan.Models.Activity({ | ||
'content': $chatbox.val(), | ||
'action': 'message', | ||
'channel_id': $chatbox.data('active_channel_id') | ||
}) | ||
|
||
activity.save({success: ()-> | ||
console.log "posted! enjoy your day" | ||
}) | ||
|
||
|
||
render: ()-> | ||
$(@el).html(@template()) | ||
@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<input type="text" class="chatbox"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
class ActivityObserver < ActiveRecord::Observer | ||
|
||
def after_save(activity) | ||
faye_channel = "/channels/#{activity.channel.to_param}/messages" | ||
Kandan::Config.broadcaster.broadcast(faye_channel, activity.attributes) | ||
faye_channel = "/channels/#{activity.channel.to_param}" | ||
# TODO move this to a rabl template | ||
broadcast_data = activity.attributes.merge({:user => activity.user.attributes}) | ||
Kandan::Config.broadcaster.broadcast(faye_channel, broadcast_data) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters