Permalink
Browse files

Update stuff to the latest chaplin.

  • Loading branch information...
1 parent 6fb5e2f commit e89f2eb7efaad18f3d766b4a8413b0eab3777c21 @paulmillr paulmillr committed Oct 8, 2012
View
@@ -24,7 +24,7 @@ module.exports = class Application extends Chaplin.Application
@initControllers()
# Register all routes and start routing
- @initRouter routes
+ @initRouter routes, pushState: false
# You might pass Router/History options as the second parameter.
# Chaplin enables pushState per default and Backbone uses / as
# the root per default. You might change that in the options
@@ -0,0 +1,6 @@
+Controller = require 'controllers/base/controller'
+
+module.exports = class LoginsController extends Controller
+ logout: ->
+ @publishEvent '!logout'
+ @publishEvent '!router:route', '/'
@@ -1,5 +1,4 @@
Controller = require 'controllers/base/controller'
-mediator = require 'mediator'
Navigation = require 'models/navigation'
NavigationView = require 'views/navigation_view'
@@ -8,10 +7,5 @@ module.exports = class NavigationController extends Controller
initialize: ->
super
- #console.debug 'NavigationController#initialize'
- @model = new Navigation()
- @view = new NavigationView model: @model
-
- logout: ->
- mediator.publish '!logout'
- Backbone.history.navigate('//')
+ @model = new Navigation
+ @view = new NavigationView {@model}
@@ -9,7 +9,7 @@ module.exports = class SessionController extends Controller
# This just hardcoded here to avoid async loading of service providers.
# In the end you might want to do this.
@serviceProviders = {
- twitter: new Twitter()
+ twitter: new Twitter
}
# Was the login status already determined?
@@ -70,11 +70,11 @@ module.exports = class SessionController extends Controller
# Publish an event in case the provider library could not be loaded
unless serviceProvider.isLoaded()
- mediator.publish 'serviceProviderMissing', serviceProviderName
+ @publishEvent 'serviceProviderMissing', serviceProviderName
return
# Publish a global loginAttempt event
- mediator.publish 'loginAttempt', serviceProviderName
+ @publishEvent 'loginAttempt', serviceProviderName
# Delegate to service provider
serviceProvider.triggerLogin()
@@ -99,16 +99,16 @@ module.exports = class SessionController extends Controller
@loginStatusDetermined = true
# Publish a global login event passing the user
- mediator.publish 'login', mediator.user
- mediator.publish 'loginStatus', true
+ @publishEvent 'login', mediator.user
+ @publishEvent 'loginStatus', true
# Logout
# ------
# Handler for the global !logout event
triggerLogout: ->
# Just publish a logout event for now
- mediator.publish 'logout'
+ @publishEvent 'logout'
# Handler for the global logout event
logout: =>
@@ -122,7 +122,7 @@ module.exports = class SessionController extends Controller
# Show the login view again
@showLoginView()
- mediator.publish 'loginStatus', false
+ @publishEvent 'loginStatus', false
# Handler for the global userData event
# -------------------------------------
@@ -6,6 +6,5 @@ module.exports = class TweetsController extends Controller
historyURL: ''
index: (params) ->
- #console.debug 'PostsController#index'
- @collection = new Tweets()
- @view = new TweetsView collection: @collection
+ @collection = new Tweets
+ @view = new TweetsView {@collection}
@@ -2,9 +2,8 @@ utils = require 'lib/utils'
Chaplin = require 'chaplin'
module.exports = class ServiceProvider
-
- # Mixin a Subscriber
- _(@prototype).extend Chaplin.Subscriber
+ # Mixin an Event Broker
+ _(@prototype).extend Chaplin.EventBroker
loading: false
@@ -62,24 +61,24 @@ module.exports = class ServiceProvider
eventPayload = {provider: this, loginContext}
if response
# Publish successful login
- mediator.publish 'loginSuccessful', eventPayload
+ @publishEvent 'loginSuccessful', eventPayload
# Publish the session
- mediator.publish 'serviceProviderSession',
+ @publishEvent 'serviceProviderSession',
provider: this
userId: response.userId
accessToken: response.accessToken
# etc.
else
- mediator.publish 'loginFail', eventPayload
+ @publishEvent 'loginFail', eventPayload
getLoginStatus: (callback = @loginStatusHandler, force = false) ->
ServiceProviderLibrary.getLoginStatus callback, force
loginStatusHandler: (response) =>
return unless response
- mediator.publish 'serviceProviderSession',
+ @publishEvent 'serviceProviderSession',
provider: this
userId: response.userId
accessToken: response.accessToken
@@ -20,30 +20,30 @@ module.exports = class Twitter extends ServiceProvider
@loading = false
# Init the SDK, then resolve
twttr.anywhere (T) =>
- mediator.publish 'sdkLoaded'
+ @publishEvent 'sdkLoaded'
@T = T
@resolve()
isLoaded: ->
# Return a Boolean
Boolean window.twttr
- publish: (event, callback) ->
+ trigger: (event, callback) ->
@T.trigger event, callback
- subscribe: (event, callback) ->
+ on: (event, callback) ->
@T.bind event, callback
- unsubscribe: (event) ->
+ off: (event) ->
@T.unbind event
# Trigger login popup
triggerLogin: (loginContext) ->
callback = _(@loginHandler).bind(this, loginContext)
@T.signIn()
- @subscribe 'authComplete', (event, currentUser, accessToken) ->
+ @on 'authComplete', (event, currentUser, accessToken) ->
callback {currentUser, accessToken}
- @subscribe 'signOut', ->
+ @on 'signOut', ->
console.log 'Signout event'
callback()
@@ -52,24 +52,24 @@ module.exports = class Twitter extends ServiceProvider
publishSession: (response) ->
user = response.currentUser
- mediator.publish 'serviceProviderSession',
+ @publishEvent 'serviceProviderSession',
provider: this
userId: user.id
accessToken: response.accessToken or twttr.anywhere.token
- mediator.publish 'userData', user.attributes
+ @publishEvent 'userData', user.attributes
# Callback for the login popup
loginHandler: (loginContext, response) =>
console.debug 'Twitter#loginHandler', loginContext, response
if response
# Publish successful login
- mediator.publish 'loginSuccessful',
+ @publishEvent 'loginSuccessful',
provider: this, loginContext: loginContext
# Publish the session
@publishSession response
else
- mediator.publish 'loginFail', provider: this, loginContext: loginContext
+ @publishEvent 'loginFail', provider: this, loginContext: loginContext
getLoginStatus: (callback = @loginStatusHandler, force = false) ->
console.debug 'Twitter#getLoginStatus'
@@ -80,7 +80,7 @@ module.exports = class Twitter extends ServiceProvider
if response.currentUser
@publishSession response
else
- mediator.publish 'logout'
+ @publishEvent 'logout'
# Handler for the global logout event
logout: ->
View
@@ -18,6 +18,6 @@ module.exports = class Status extends Model
timeout = setTimeout options.error.bind(options, 'Timeout error'), 4000
provider.T.Status.update model.get('text'), (tweet) =>
window.clearTimeout(timeout)
- mediator.publish 'tweet:add', tweet.attributes
+ @publishEvent 'tweet:add', tweet.attributes
options.success(tweet.attributes)
- return
+ return
View
@@ -4,7 +4,7 @@ Model = require 'models/base/model'
module.exports = class User extends Model
initialize: ->
super
- mediator.on 'userMethods', @initializeMethods
+ @subscribeEvent 'userMethods', @initializeMethods
# twttr.anywhere has many useful methods like isFollowedBy()
# so it's great to have them in the model.
View
@@ -1,4 +1,4 @@
module.exports = (match) ->
+ match 'logout', 'login#logout'
match '', 'tweets#index'
match '@:user', 'user#show'
- match 'logout', 'navigation#logout'
@@ -1,22 +0,0 @@
-View = require 'views/base/view'
-
-module.exports = class CompositeView extends View
- initialize: ->
- super
- @subViews = []
-
- attachView: (view) ->
- @subViews.push view
-
- renderSubViews: ->
- _(@subViews).forEach (view) =>
- @$(view.container).append view.render().el
-
- render: =>
- super
- @renderSubViews()
-
- dispose: =>
- super
- _(@subViews).forEach (view) =>
- view.dispose()
View
@@ -1,6 +1,3 @@
Chaplin = require 'chaplin'
module.exports = class Layout extends Chaplin.Layout
- initialize: ->
- super
- # @subscribeEvent 'startupController', @doSomething
@@ -1,30 +1,22 @@
-mediator = require 'mediator'
utils = require 'lib/utils'
View = require 'views/base/view'
template = require 'views/templates/login'
module.exports = class LoginView extends View
- # This is a workaround.
- # In the end you might want to used precompiled templates.
template: template
-
id: 'login'
container: '#content-container'
autoRender: true
# Expects the serviceProviders in the options
initialize: (options) ->
super
- console.debug 'LoginView#initialize', @el, @$el, options, options.serviceProviders
@initButtons options.serviceProviders
# In this project we currently only have one service provider and therefore
# one button. But this should allow for different service providers.
initButtons: (serviceProviders) ->
- console.debug 'LoginView#initButtons', serviceProviders
-
for serviceProviderName, serviceProvider of serviceProviders
-
buttonSelector = ".#{serviceProviderName}"
@$(buttonSelector).addClass('service-loading')
@@ -43,19 +35,18 @@ module.exports = class LoginView extends View
)
serviceProvider.fail failed
- loginWith: (serviceProviderName, serviceProvider, e) ->
- console.debug 'LoginView#loginWith', serviceProviderName, serviceProvider
- e.preventDefault()
+ loginWith: (serviceProviderName, serviceProvider, event) ->
+ event.preventDefault()
return unless serviceProvider.isLoaded()
- mediator.publish 'login:pickService', serviceProviderName
- mediator.publish '!login', serviceProviderName
+ @publishEvent 'login:pickService', serviceProviderName
+ @publishEvent '!login', serviceProviderName
serviceProviderLoaded: (serviceProviderName) ->
- #console.debug 'LoginView#serviceProviderLoaded', serviceProviderName
+ return if @disposed
@$(".#{serviceProviderName}").removeClass('service-loading')
serviceProviderFailed: (serviceProviderName) ->
- #console.debug 'LoginView#serviceProviderFailed', serviceProviderName
+ return if @disposed
@$(".#{serviceProviderName}")
.removeClass('service-loading')
.addClass('service-unavailable')
@@ -1,4 +1,3 @@
-mediator = require 'mediator'
View = require 'views/base/view'
template = require 'views/templates/navigation'
@@ -13,6 +12,5 @@ module.exports = class NavigationView extends View
initialize: ->
super
- #console.debug 'NavigationView#initialize'
@subscribeEvent 'loginStatus', @render
@subscribeEvent 'startupController', @render
@@ -1,10 +1,10 @@
mediator = require 'mediator'
-CompositeView = require 'views/composite_view'
+View = require 'views/base/view'
StatsView = require 'views/stats_view'
StatusView = require 'views/status_view'
template = require 'views/templates/sidebar'
-module.exports = class SidebarView extends CompositeView
+module.exports = class SidebarView extends View
template: template
id: 'sidebar'
@@ -13,11 +13,16 @@ module.exports = class SidebarView extends CompositeView
initialize: ->
super
- @attachView new StatusView()
- @attachView new StatsView()
@subscribeEvent 'loginStatus', @loginStatusHandler
@subscribeEvent 'userData', @render
loginStatusHandler: (loggedIn) =>
@model = if loggedIn then mediator.user else null
@render()
+
+ render: ->
+ super
+ @subview 'status', new StatusView container: @$('#status-container')
+ @subview 'stats', new StatsView container: @$('#stats-container')
+ ['status', 'stats'].forEach (name) =>
+ @subview(name).render()
@@ -6,16 +6,13 @@ module.exports = class StatsView extends View
template: template
className: 'stats'
tagName: 'ul'
- container: '#stats-container'
initialize: ->
super
@subscribeEvent 'loginStatus', @loginStatusHandler
@subscribeEvent 'userData', @render
+ @model = if mediator.user then mediator.user else null
loginStatusHandler: (loggedIn) =>
- if loggedIn
- @model = mediator.user
- else
- @model = null
+ @model = if loggedIn then mediator.user else null
@render()
Oops, something went wrong.

0 comments on commit e89f2eb

Please sign in to comment.