Sha256: 595017c277732b11dccb53d89009d284aa6d315fea8f913a11f5bc2a0a234851

Contents?: true

Size: 1.75 KB

Versions: 6

Compression:

Stored size: 1.75 KB

Contents

###
The channel object is returned when you subscribe to a channel.

For instance:
  var dispatcher = new WebSocketRails('localhost:3000/websocket');
  var awesome_channel = dispatcher.subscribe('awesome_channel');
  awesome_channel.bind('event', function(data) { console.log('channel event!'); });
  awesome_channel.trigger('awesome_event', awesome_object);
###
class WebSocketRails.Channel

  constructor: (@name,@_dispatcher,@is_private) ->
    if @is_private
      event_name = 'websocket_rails.subscribe_private'
    else
      event_name = 'websocket_rails.subscribe'

    event = new WebSocketRails.Event( [event_name, {data: {channel: @name}},@_dispatcher.connection_id], @_success_launcher, @_failure_launcher)
    @_dispatcher.trigger_event event
    @_callbacks = {}

  destroy: () =>
    event_name = 'websocket_rails.unsubscribe'
    event =  new WebSocketRails.Event( [event_name, {data: {channel: @name}}, @_dispatcher.connection_id] )
    @_dispatcher.trigger_event event
    @_callbacks = {}

  bind: (event_name, callback) =>
    @_callbacks[event_name] ?= []
    @_callbacks[event_name].push callback

  trigger: (event_name, message) =>
    event = new WebSocketRails.Event( [event_name, {channel: @name, data: message}, @_dispatcher.connection_id] )
    @_dispatcher.trigger_event event

  dispatch: (event_name, message) =>
    return unless @_callbacks[event_name]?
    for callback in @_callbacks[event_name]
      callback message
  
  # using this method because @on_success will not be defined when the constructor is executed
  _success_launcher: (data) =>
    @on_success(data) if @on_success?
    
  # using this method because @on_failure will not be defined when the constructor is executed
  _failure_launcher: (data) =>
    @on_failure(data) if @on_failure?

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
websocket-rails-0.6.2 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.6.1 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.6.0 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.5.0 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.4.9 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.4.8 lib/assets/javascripts/websocket_rails/channel.js.coffee