我在Chaplin.js文档中看到了很多例子,这意味着模型/对象可以通过以下方式持久保存在中介上:
createUser: (userData) ->
mediator.user = new User userData
在我的应用程序中,每当我尝试将用户模型设置为介体上的属性时,该属性不会在路由之间保留。例如,在@onSessionSaveSuccess中,我将用户分配给介体,但是,在重定向到DashboardController并尝试访问mediator.user之后,我收到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
请帮助我理解我是如何滥用这种模式的。谢谢。
答案 0 :(得分:0)
通过@paulmillr -
@theMTA介体密封在Application#initMediator方法中。我建议你像https://t.co/3Zfgp8t86s
一样覆盖它 - 保罗米勒(@paulmillr)April 8, 2014