diff options
Diffstat (limited to 'lib/assets/javascripts/cable/channel.js.coffee')
-rw-r--r-- | lib/assets/javascripts/cable/channel.js.coffee | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/assets/javascripts/cable/channel.js.coffee b/lib/assets/javascripts/cable/channel.js.coffee new file mode 100644 index 0000000000..645a44e140 --- /dev/null +++ b/lib/assets/javascripts/cable/channel.js.coffee @@ -0,0 +1,22 @@ +class Cable.Channel + constructor: (@cable, params = {}, mixin) -> + @identifier = JSON.stringify(params) + extend(this, mixin) + @cable.subscribers.add(@identifier, this) + + # Perform a channel action with the optional data passed as an attribute + sendAction: (action, data = {}) -> + data.action = action + @sendMessage(data) + + sendMessage: (data) -> + @cable.sendMessage(@identifier, JSON.stringify(data)) + + unsubscribe: -> + @cable.subscribers.remove(@identifier) + + extend = (object, properties) -> + if properties? + for key, value of properties + object[key] = value + object |