Subscribe to pusher channels only when entering the MainRoute

We don't want to subscribe if we're at any other routes, because there is no
data there. The main target is to avoid all of the notifications from a common
channel if we're on the /dashboard route.
This commit is contained in:
Piotr Sarnacki 2015-01-15 10:36:35 +01:00
parent a891892bd8
commit 5065bb5739
10 changed files with 60 additions and 20 deletions

View File

@ -21,7 +21,6 @@ $.extend Travis.Pusher.prototype,
Pusher.warn = @warn.bind(this) Pusher.warn = @warn.bind(this)
Pusher.host = config.host if config.host Pusher.host = config.host if config.host
@pusher = new Pusher(config.key, encrypted: Travis.Pusher.ENCRYPTED, disableStats: true) @pusher = new Pusher(config.key, encrypted: Travis.Pusher.ENCRYPTED, disableStats: true)
@subscribeAll(Travis.Pusher.CHANNELS) if Travis.Pusher.CHANNELS
@callbacksToProcess = [] @callbacksToProcess = []
@ -33,6 +32,9 @@ $.extend Travis.Pusher.prototype,
subscribeAll: (channels) -> subscribeAll: (channels) ->
@subscribe(channel) for channel in channels @subscribe(channel) for channel in channels
unsubscribeAll: (channels) ->
@unsubscribe(channel) for channel in channels
subscribe: (channel) -> subscribe: (channel) ->
return unless channel return unless channel
channel = @prefix(channel) channel = @prefix(channel)

View File

@ -9,6 +9,7 @@ Ember.Router.reopen
@_super(url) @_super(url)
Travis.Router.map -> Travis.Router.map ->
@resource 'dashboard'
@resource 'main', path: '/', -> @resource 'main', path: '/', ->
@resource 'getting_started' @resource 'getting_started'
@route 'recent' @route 'recent'
@ -80,3 +81,4 @@ require 'routes/settings'
require 'routes/simple_layout' require 'routes/simple_layout'
require 'routes/ssh_key' require 'routes/ssh_key'
require 'routes/stats' require 'routes/stats'
require 'routes/dashboard'

View File

@ -0,0 +1,7 @@
require 'routes/route'
TravisRoute = Travis.Route
Route = TravisRoute.extend()
Travis.DashboardRoute = Route

View File

@ -1,6 +1,8 @@
require 'routes/route' require 'routes/route'
require 'pusher'
TravisRoute = Travis.Route TravisRoute = Travis.Route
channels = Travis.Pusher.CHANNELS
Route = TravisRoute.extend Route = TravisRoute.extend
renderTemplate: -> renderTemplate: ->
@ -15,5 +17,9 @@ Route = TravisRoute.extend
toActivate = if @signedIn() then 'owned' else 'recent' toActivate = if @signedIn() then 'owned' else 'recent'
@container.lookup('controller:repos').activate(toActivate) @container.lookup('controller:repos').activate(toActivate)
activate: ->
# subscribe to pusher only if we're at a main route
if channels
@get('pusher').subscribeAll(channels)
Travis.MainRoute = Route Travis.MainRoute = Route

View File

@ -0,0 +1 @@
DASHBOARD GOES HERE!

View File

@ -0,0 +1,5 @@
{{#link-to "main"}}Home{{/link-to}}
{{yield}}
END

View File

@ -1,4 +1,5 @@
require 'ext/ember/namespace' require 'ext/ember/namespace'
require 'views/view'
Em.View.reopen Em.View.reopen
init: -> init: ->
@ -9,25 +10,6 @@ Em.View.reopen
Travis.NotFoundView = Ember.View.extend Travis.NotFoundView = Ember.View.extend
layoutName: 'layouts/simple' layoutName: 'layouts/simple'
@Travis.reopen
View: Em.View.extend
actions:
popup: (name) -> @popup(name)
popupClose: -> @popupClose()
popup: (name) ->
@popupCloseAll()
name = event?.target?.name || name
$("##{name}").toggleClass('display')
popupClose: ->
$('.popup').removeClass('display')
popupCloseAll: ->
if view = Travis.View.currentPopupView
view.destroy()
Travis.View.currentPopupView = null
$('.popup').removeClass('display')
Travis.MainView = Travis.View.extend Travis.MainView = Travis.View.extend
layoutName: 'layouts/home' layoutName: 'layouts/home'
classNames: ['application'] classNames: ['application']
@ -120,3 +102,4 @@ require 'views/signin'
require 'views/top' require 'views/top'
require 'views/status_images' require 'views/status_images'
require 'views/status_image_input' require 'views/status_image_input'
require 'views/dashboard'

View File

@ -0,0 +1,8 @@
require 'views/view'
TravisView = Travis.View
View = TravisView.extend
layoutName: 'layouts/dashboard'
Travis.DashboardView = View

View File

@ -0,0 +1,19 @@
View = Ember.View.extend
actions:
popup: (name) -> @popup(name)
popupClose: -> @popupClose()
popup: (name) ->
@popupCloseAll()
name = event?.target?.name || name
$("##{name}").toggleClass('display')
popupClose: ->
$('.popup').removeClass('display')
popupCloseAll: ->
if view = Travis.View.currentPopupView
view.destroy()
Travis.View.currentPopupView = null
$('.popup').removeClass('display')
Travis.View = View

View File

@ -182,6 +182,13 @@ Travis.initializer
application.inject('controller', 'config', 'config:main') application.inject('controller', 'config', 'config:main')
application.inject('route', 'config', 'config:main') application.inject('route', 'config', 'config:main')
Travis.initializer
name: 'inject-pusher'
initialize: (container, application) ->
application.register 'pusher:main', Travis.pusher, { instantiate: false }
application.inject('route', 'pusher', 'pusher:main')
Travis.Router.reopen Travis.Router.reopen
didTransition: -> didTransition: ->