Skip to content

Commit

Permalink
Fakes faster messaging by clearing the textbox before saving the message
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Manohar J <akash@akash.im>
  • Loading branch information
HashNuke committed Apr 13, 2012
1 parent d7dac42 commit 33e3fc0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Kandan.Broadcasters.FayeBroadcaster

subscribe: (channel)->
subscription = @fayeClient.subscribe channel, (data)=>
Kandan.Helpers.Channels.addActivity(data)
Kandan.Helpers.Channels.addActivity(data, Kandan.Helpers.Activities.ACTIVE_STATE)
subscription.errback((data)->
console.log "error", data
alert "Oops! could not connect to the server"
Expand Down
26 changes: 17 additions & 9 deletions app/assets/javascripts/backbone/helpers/channels.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ class Kandan.Helpers.Channels
scrollPercentage > @options.autoScrollThreshold

@scrollToLatestMessage: (channelId)->
console.log("scrolling to last message")
$(document).scrollTop($(document).height()+9000)

@currentScrollPosition: (channelId)->
console.log("current scroll position")
$(document).scrollTop()

@channelActivitiesEl: (channelId)->
Expand Down Expand Up @@ -104,23 +102,33 @@ class Kandan.Helpers.Channels
@createChannelArea(new Kandan.Models.Channel(activityAttributes.channel))


@addActivity: (activityAttributes, state)->
@addActivity: (activityAttributes, state, local)->
local = local || false
@createChannelIfNotExists(activityAttributes)

if activityAttributes.channel_id
@addMessage(activityAttributes, state)
@addMessage(activityAttributes, state, local)
else
@addNotification(activityAttributes)

channelId = activityAttributes.channel_id || @getActiveChannelId()
@scrollToLatestMessage(channelId) if @pastAutoScrollThreshold(channelId)

@addMessage: (activityAttributes, state)->
@channelActivitiesEl(activityAttributes.channel_id)
.append(@newActivityView(activityAttributes).render().el)

@addMessage: (activityAttributes, state, local)->
belongsToCurrentUser = ( activityAttributes.user.id == Kandan.Data.Users.currentUser().id )
activityExists = ( $("#activity-#{activityAttributes.id}").length == 0 )
local = local || false

console.log local, !belongsToCurrentUser, !activityExists
if local || (!belongsToCurrentUser || !activityExists)
@channelActivitiesEl(activityAttributes.channel_id)
.append(@newActivityView(activityAttributes).render().el)

@flushActivities(activityAttributes.channel_id)
Kandan.Helpers.Utils.notifyInTitleIfRequired()
@setPaginationData(activityAttributes.channel_id)
if not local
Kandan.Helpers.Utils.notifyInTitleIfRequired()
@setPaginationData(activityAttributes.channel_id)


@addNotification: (activityAttributes)->
Expand Down
7 changes: 5 additions & 2 deletions app/assets/javascripts/backbone/views/chatbox.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ class Kandan.Views.Chatbox extends Backbone.View
'channel_id': @channel.get('id')
})

activity.save({},{success: ()=>
$chatbox.val("")
$chatbox.val("")
Kandan.Helpers.Channels.addActivity _.extend(activity.toJSON(), {cid: activity.cid, user: Kandan.Data.Users.currentUser()}), Kandan.Helpers.Activities.ACTIVE_STATE, true

activity.save({},{success: (model, response)->
$("#activity-c#{model.cid}").attr("id", "activity-#{model.get('id')}")
})

render: ()->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class Kandan.Views.ShowActivity extends Backbone.View
@compiledTemplate = Kandan.Helpers.Activities.buildFromMessageTemplate activity

$(@el).data("activity-id", activity.id)
$(@el).attr("id", "activity-#{activity.id}")
if activity.id == undefined
$(@el).attr("id", "activity-c#{activity.cid}")
else
$(@el).attr("id", "activity-#{activity.id}")
$(@el).html(@compiledTemplate)

#NOTE can only set the data after it's been appended to the DOM
Expand Down

0 comments on commit 33e3fc0

Please sign in to comment.