Sha256: e45a8b86ced87c07a57052d31a50d2b5e14abab4c26731fb1a279118e631e9e4

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

angular.module('EssayApp.services')
  .factory('PusherService', ["$http", ($http) ->
    api = {
      pusher: undefined
      channel: 'materials@{{public_token}}'
    }

    $.getScript('https://js.pusher.com/3.2/pusher.min.js')

    api.subscribe = (public_token, params = {})->
      _onParse = params.onParse
      _onConnect = params.onConnect
      _onFail = params.onFail

      api.pusher ||= new Pusher('61187c996681789fd857',
        encrypted: true
      )
      channel = api.pusher.subscribe(api.channel.replace('{{public_token}}', public_token))

      if angular.isFunction(_onConnect)
        channel.bind 'pusher:subscription_succeeded', ->
          _onConnect.call(undefined)

      if angular.isFunction(_onFail)
        channel.bind 'pusher:subscription_error', (status)->
          _onFail.call(undefined, status)

      channel.bind 'parsed', (data)->
        if angular.isFunction(_onParse)
          _onParse.call(undefined, data)
        else
          console.log 'recived', data

    api.unsubscribe = (public_token)->
      api.pusher.unsubscribe(api.channel.replace('{{public_token}}', public_token))

    return api
  ])

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
translation_cms-0.1.5 app/assets/javascripts/app/services/pusher_service.js.coffee