Use omit and add example

This commit is contained in:
Sébastien Chopin 2016-11-17 14:02:09 +01:00
parent c9250b4d07
commit 06edc8029b
3 changed files with 19 additions and 3 deletions

View File

@ -73,3 +73,19 @@ export default {
## Context
To see the list of available keys in `context`, take a look at [this documentation](https://github.com/nuxt/nuxt.js/tree/master/examples/async-data#context).
## Action `nuxtServerInit`
If you define the action `nuxtServerInit` in your store, nuxt.js will call it with the context, it can be useful when having some data on the server you want to give to the client-side, for example, the authenticated user:
```js
// store/index.js
actions: {
nuxtServerInit ({ commit }, { req }) {
if (req.authUser) {
commit('user', req.authUser)
}
}
}
```
The context given to `nuxtServerInit` is the same as the `data` of `fetch` method except `context.redirect()` and `context.error()` are omitted.

View File

@ -3,7 +3,7 @@
const debug = require('debug')('nuxt:render')
import Vue from 'vue'
import { stringify } from 'querystring'
import { pick } from 'lodash'
import { omit } from 'lodash'
import { app, router<%= (store ? ', store' : '') %> } from './index'
import { getMatchedComponents, getContext, urlJoin } from './utils'
@ -50,7 +50,7 @@ export default context => {
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
<% if (store) { %>
let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', pick(getContext(context), 'req', 'res', 'params', 'query')) : null)
let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', omit(getContext(context), 'redirect', 'error')) : null)
if (!(promise instanceof Promise)) promise = Promise.resolve()
<% } else { %>
let promise = Promise.resolve()

View File

@ -1,6 +1,6 @@
{
"name": "nuxt",
"version": "0.4.5",
"version": "0.4.6",
"description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)",
"main": "index.js",
"license": "MIT",