From cd420c20b577802e08cd3faf7ca8f7532384481c Mon Sep 17 00:00:00 2001 From: Julie Date: Fri, 26 Feb 2016 17:01:27 -0500 Subject: [PATCH 1/3] More commenting --- .../javascripts/channels/comments.coffee | 28 ++++++++++++++----- cable/config.ru | 4 +++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/channels/comments.coffee b/app/assets/javascripts/channels/comments.coffee index 4445716b..3796cb70 100644 --- a/app/assets/javascripts/channels/comments.coffee +++ b/app/assets/javascripts/channels/comments.coffee @@ -1,6 +1,5 @@ App.comments = App.cable.subscriptions.create "CommentsChannel", - collection: -> $("[data-channel='comments']") - + # Called when the subscription is ready for use on the server connected: -> # FIXME: While we wait for cable subscriptions to always be finalized before sending messages setTimeout => @@ -8,19 +7,34 @@ App.comments = App.cable.subscriptions.create "CommentsChannel", @installPageChangeCallback() , 1000 - received: (data) -> - @collection().append(data.comment) unless @userIsCurrentUser(data.comment) + # Called when the WebSocket connection is closed + disconnected: -> + # TODO hide messages - userIsCurrentUser: (comment) -> - $(comment).attr('data-user-id') is $('meta[name=current-user]').attr('id') + # Called when the subscription is rejected by the server + rejected: -> + # TODO hide messages + + # When data is received from the server itself + received: (data) -> + @_commentsDiv().append(data.comment) unless @_userIsCurrentUser(data.comment) followCurrentMessage: -> - if messageId = @collection().data('message-id') + if messageId = @_commentsDiv().data('message-id') + # corresponds to CommentsChannel#follow @perform 'follow', message_id: messageId else + # corresponds to CommentsChannel#unfollow @perform 'unfollow' installPageChangeCallback: -> unless @installedPageChangeCallback @installedPageChangeCallback = true $(document).on 'page:change', -> App.comments.followCurrentMessage() + + # Helpers + + _userIsCurrentUser: (comment) -> + $(comment).attr('data-user-id') is $('meta[name=current-user]').attr('id') + + _commentsDiv: -> $("[data-channel='comments']") \ No newline at end of file diff --git a/cable/config.ru b/cable/config.ru index 341e724e..6f109b98 100644 --- a/cable/config.ru +++ b/cable/config.ru @@ -1,4 +1,8 @@ require ::File.expand_path('../../config/environment', __FILE__) Rails.application.eager_load! +# require 'action_cable/process/logging' + +ActionCable.server.config.allowed_request_origins = ["http://localhost:3003"] + run ActionCable.server From 0f582f292753bbaad45bd74487f851829c2e6877 Mon Sep 17 00:00:00 2001 From: Julie Date: Sat, 27 Feb 2016 08:57:16 -0500 Subject: [PATCH 2/3] remove unused code --- app/assets/javascripts/channels/comments.coffee | 3 ++- cable/config.ru | 4 ---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/channels/comments.coffee b/app/assets/javascripts/channels/comments.coffee index 3796cb70..5e98a41a 100644 --- a/app/assets/javascripts/channels/comments.coffee +++ b/app/assets/javascripts/channels/comments.coffee @@ -1,7 +1,8 @@ App.comments = App.cable.subscriptions.create "CommentsChannel", # Called when the subscription is ready for use on the server connected: -> - # FIXME: While we wait for cable subscriptions to always be finalized before sending messages + # FIXME: While we wait for cable subscriptions to always be finalized + # before sending messages setTimeout => @followCurrentMessage() @installPageChangeCallback() diff --git a/cable/config.ru b/cable/config.ru index 6f109b98..341e724e 100644 --- a/cable/config.ru +++ b/cable/config.ru @@ -1,8 +1,4 @@ require ::File.expand_path('../../config/environment', __FILE__) Rails.application.eager_load! -# require 'action_cable/process/logging' - -ActionCable.server.config.allowed_request_origins = ["http://localhost:3003"] - run ActionCable.server From 7a2e40b0cf1d6180e185d13ff1a54a9e6975547a Mon Sep 17 00:00:00 2001 From: Julie Date: Mon, 29 Feb 2016 14:42:22 -0500 Subject: [PATCH 3/3] remove underscores --- app/assets/javascripts/channels/comments.coffee | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/channels/comments.coffee b/app/assets/javascripts/channels/comments.coffee index 5e98a41a..0a7d269e 100644 --- a/app/assets/javascripts/channels/comments.coffee +++ b/app/assets/javascripts/channels/comments.coffee @@ -1,8 +1,7 @@ App.comments = App.cable.subscriptions.create "CommentsChannel", # Called when the subscription is ready for use on the server connected: -> - # FIXME: While we wait for cable subscriptions to always be finalized - # before sending messages + # FIXME: While we wait for cable subscriptions to always be finalized before sending messages setTimeout => @followCurrentMessage() @installPageChangeCallback() @@ -18,10 +17,10 @@ App.comments = App.cable.subscriptions.create "CommentsChannel", # When data is received from the server itself received: (data) -> - @_commentsDiv().append(data.comment) unless @_userIsCurrentUser(data.comment) + @commentsDiv().append(data.comment) unless @userIsCurrentUser(data.comment) followCurrentMessage: -> - if messageId = @_commentsDiv().data('message-id') + if messageId = @commentsDiv().data('message-id') # corresponds to CommentsChannel#follow @perform 'follow', message_id: messageId else @@ -35,7 +34,7 @@ App.comments = App.cable.subscriptions.create "CommentsChannel", # Helpers - _userIsCurrentUser: (comment) -> + userIsCurrentUser: (comment) -> $(comment).attr('data-user-id') is $('meta[name=current-user]').attr('id') - _commentsDiv: -> $("[data-channel='comments']") \ No newline at end of file + commentsDiv: -> $("[data-channel='comments']") \ No newline at end of file