Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there an other way to transform the session and flash ? #20

Closed
uinz opened this issue Jun 12, 2016 · 3 comments
Closed

Is there an other way to transform the session and flash ? #20

uinz opened this issue Jun 12, 2016 · 3 comments

Comments

@uinz
Copy link

uinz commented Jun 12, 2016

should I set it in all render function?

likes global locals

ctx.render('sign/signin', {
  session: ctx.session,
  flash: ctx.flash
})
@uinz
Copy link
Author

uinz commented Jun 12, 2016

I got it !
#1
add a middleware

app.use(async (ctx, next) => {
    const oldRender = ctx.render
    const user = ctx.session.user || null
    const flash = ctx.session.flash

    ctx.render = async (tpl, locals, options, noCache) => {
        const data = _.merge({ user, flash }, locals)
        console.log(data)
        await oldRender.call(ctx, tpl, data, options, noCache)
    }

    await next()
})

@uinz uinz closed this as completed Jun 12, 2016
@chrisyip
Copy link
Owner

You don't need to create a new ctx.render, use ctx.state instead:

app.use((ctx, next) => {
  ...
  ctx.state.session = ctx.session
  ...
})

app.use((ctx) => {
  ctx.render('tpl') // session will be available in view automatically
})

@uinz
Copy link
Author

uinz commented Jun 12, 2016

thanks much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants