aboutsummaryrefslogtreecommitdiffstats
path: root/lib/assets/javascripts/channel.js.coffee
diff options
context:
space:
mode:
authorJavan Makhmali <javan@javan.us>2015-06-24 14:26:26 -0400
committerJavan Makhmali <javan@javan.us>2015-06-24 14:26:26 -0400
commit268ee5208ce513eb0b74e2354259e7991d1633c9 (patch)
treea2ad2d5c0d68e0afda949df01ad9505fe95ec610 /lib/assets/javascripts/channel.js.coffee
parent2ecc9b513821a7ebf365f42f2061e4e4300cf6d3 (diff)
downloadrails-268ee5208ce513eb0b74e2354259e7991d1633c9.tar.gz
rails-268ee5208ce513eb0b74e2354259e7991d1633c9.tar.bz2
rails-268ee5208ce513eb0b74e2354259e7991d1633c9.zip
Create JavaScript channels identified by their Ruby class name
Diffstat (limited to 'lib/assets/javascripts/channel.js.coffee')
-rw-r--r--lib/assets/javascripts/channel.js.coffee43
1 files changed, 18 insertions, 25 deletions
diff --git a/lib/assets/javascripts/channel.js.coffee b/lib/assets/javascripts/channel.js.coffee
index c972334140..8bca24bd0e 100644
--- a/lib/assets/javascripts/channel.js.coffee
+++ b/lib/assets/javascripts/channel.js.coffee
@@ -1,33 +1,26 @@
class @Cable.Channel
- constructor: (params = {}) ->
- {channelName} = @constructor
+ constructor: (@cable, params = {}, mixin) ->
+ @identifier = JSON.stringify(params)
+ extend(this, mixin)
- if channelName?
- params['channel'] = channelName
- @channelIdentifier = JSON.stringify params
- else
- throw new Error "This channel's constructor is missing the required 'channelName' property"
-
- cable.subscribe(@channelIdentifier, {
- onConnect: @connected
- onDisconnect: @disconnected
- onReceiveData: @received
- })
-
-
- connected: =>
- # Override in the subclass
-
- disconnected: =>
- # Override in the subclass
-
- received: (data) =>
- # Override in the subclass
+ @cable.subscribe @identifier,
+ onConnect: => @connected?()
+ onDisconnect: => @disconnected?()
+ onReceiveData: (data) => @receive?(data)
# Perform a channel action with the optional data passed as an attribute
perform: (action, data = {}) ->
data.action = action
- cable.sendData @channelIdentifier, JSON.stringify data
+ @cable.sendData(@identifier, JSON.stringify(data))
send: (data) ->
- cable.sendData @channelIdentifier, JSON.stringify data
+ @cable.sendData(@identifier, JSON.stringify(data))
+
+ close: ->
+ @cable.unsubscribe(@identifier)
+
+ extend = (object, properties) ->
+ if properties?
+ for key, value of properties
+ object[key] = value
+ object