Sha256: 26caf2b7e715e19bc13a0069e326150db1b63d815f825cfbf6176c2d4adb74c8

Contents?: true

Size: 1.52 KB

Versions: 15

Compression:

Stored size: 1.52 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 = {}

  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

15 entries across 15 versions & 1 rubygems

Version Path
websocket-rails-0.4.7 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.4.6 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.4.5 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.4.4 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.4.3 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.4.2 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.4.1 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.4.0 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.3.0 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.2.1 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.2.0 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.1.9 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.1.8 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.1.7 lib/assets/javascripts/websocket_rails/channel.js.coffee
websocket-rails-0.1.6 lib/assets/javascripts/websocket_rails/channel.js.coffee