I've seen many examples in the Chaplin.js docs, which imply that a model/object can be persisted on the mediator in the following fashion:
createUser: (userData) ->
mediator.user = new User userData
In my application, whenever I attempt to set a user model as a property on mediator, the property does not persist between routes. For example, in @onSessionSaveSuccess I assign a user to the mediator, however, after redirecting to the DashboardController, and attempting to access the mediator.user, I receive undefined
.
AuthController = require 'controllers/auth-controller'
SiteView = require 'views/site-view'
LoginView = require 'views/login/login-view'
Session = require 'models/session'
User = require 'models/user'
mediator = require 'mediator'
module.exports = class SessionController extends AuthController
initialize: ->
@subscribeEvent 'login', @triggerLogin
@subscribeEvent 'logout', @triggerLogout
beforeAction: ->
super
@reuse 'site', SiteView
index: (params, route, options) ->
if @authenticated()
Chaplin.utils.redirectTo 'dashboard#index'
else
@view = new LoginView region: 'main'
@view.displayErrors() if params.error
logout: (params, route, options) ->
@triggerLogout()
triggerLogin: (options) ->
if @authenticated()
Chaplin.utils.redirectTo 'dashboard#index'
else
@session.save
userId: options.username
password: options.passphrase
,
success: @onSessionSaveSuccess
error: @onSessionSaveError
triggerLogout: ->
$.removeCookie 'userId'
$.removeCookie 'accessToken'
@session = new Session
@session.destroy()
onSessionSaveSuccess: (model, response, options) ->
if /error|failure/i.test response.status
Chaplin.utils.redirectTo 'session#index', error: true
else if /success/i.test response.status
$.cookie('userId', model.get('userId'), path: '/')
$.cookie('accessToken', model.get('token'), path: '/')
@user = new User model.attributes
mediator.user = @user
Chaplin.utils.redirectTo 'dashboard#index'
onSessionSaveError: (model, response, options) ->
Chaplin.utils.redirectTo 'session#index', error: true
Please help me understand how I am misusing this pattern. Thank you.
via @paulmillr —
@theMTA mediator is sealed in Application#initMediator method. I suggest you to override it like here https://t.co/3Zfgp8t86s
— Paul Miller (@paulmillr) April 8, 2014