aboutsummaryrefslogtreecommitdiffstats
path: root/lib/assets/javascripts/channel.js.coffee
blob: 058bcc03aab247f99dffcd2c4331f181ede4e327 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class @Cable.Channel
  constructor: (params = {}) ->
    @channelName ?= @underscore @constructor.name

    params['channel'] = @channelName
    @channelIdentifier = JSON.stringify params

    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

  send: (data) ->
    cable.sendData @channelIdentifier, JSON.stringify data

  underscore: (value) ->
    value.replace(/[A-Z]/g, (match) => "_#{match.toLowerCase()}").substr(1)