Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
node-sample
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
周韬
node-sample
Commits
e7014499
Commit
e7014499
authored
Aug 11, 2016
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add some comments
parent
d7581f75
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
34 additions
and
6 deletions
+34
-6
server.js
server.js
+2
-0
app.js
src/app.js
+10
-1
client-entry.js
src/client-entry.js
+3
-1
server-entry.js
src/server-entry.js
+17
-3
CreateListView.js
src/views/CreateListView.js
+2
-1
No files found.
server.js
View file @
e7014499
...
...
@@ -7,6 +7,8 @@ const resolve = file => path.resolve(__dirname, file)
const
express
=
require
(
'express'
)
const
favicon
=
require
(
'serve-favicon'
)
const
serialize
=
require
(
'serialize-javascript'
)
// https://github.com/vuejs/vue/blob/next/packages/vue-server-renderer/README.md#why-use-bundlerenderer
const
createBundleRenderer
=
require
(
'vue-server-renderer'
).
createBundleRenderer
const
app
=
express
()
...
...
src/app.js
View file @
e7014499
...
...
@@ -5,16 +5,25 @@ import router from './router'
import
{
sync
}
from
'vuex-router-sync'
import
*
as
filters
from
'./filters'
// sync the router with the vuex store.
// this registers `store.state.route`
sync
(
store
,
router
)
// register global utility filters.
Object
.
keys
(
filters
).
forEach
(
key
=>
{
Vue
.
filter
(
key
,
filters
[
key
])
})
// create the app instance.
// here we inject the router and store to all child components,
// making them available everywhere as `this.$router` and `this.$store`.
const
app
=
new
Vue
({
router
,
store
,
...
App
...
App
// Object spread copying everything from App.vue
})
// expose the app, the router and the store.
// note we not mounting the app here, since bootstrapping will be
// different depending on whether we are in browser or on the server.
export
{
app
,
router
,
store
}
src/client-entry.js
View file @
e7014499
require
(
'es6-promise'
).
polyfill
()
import
{
app
,
store
}
from
'./app'
// prime the store with server-initialized state
// prime the store with server-initialized state.
// the state is determined during SSR and inlined in the page markup.
store
.
replaceState
(
window
.
__INITIAL_STATE__
)
// actually mount to DOM
app
.
$mount
(
'#app'
)
src/server-entry.js
View file @
e7014499
...
...
@@ -2,19 +2,33 @@ import { app, router, store } from './app'
const
isDev
=
process
.
env
.
NODE_ENV
!==
'production'
// This exported function will be called by `bundleRenderer`.
// This is where we perform data-prefetching to determine the
// state of our application before actually rendering it.
// Since data fetching is async, this function is expected to
// return a Promise that resolves to the app instance.
export
default
context
=>
{
// set router's location
router
.
push
(
context
.
url
)
// call prefetch hooks on components matched by the route
const
s
=
isDev
&&
Date
.
now
()
// Call preFetch hooks on components matched by the route.
// A preFetch hook dispatches a store action and returns a Promise,
// which is resolved when the action is complete and store state has been
// updated.
return
Promise
.
all
(
router
.
getMatchedComponents
().
map
(
component
=>
{
if
(
component
.
preFetch
)
{
return
component
.
preFetch
(
store
)
}
})).
then
(()
=>
{
isDev
&&
console
.
log
(
`data pre-fetch:
${
Date
.
now
()
-
s
}
ms`
)
// set initial store on context
// the request handler will inline the state in the HTML response.
// After all preFetch hooks are resolved, our store is now
// filled with the needed state to render the app.
// Expose the state on the render context, and let the request handler
// inline the state in the HTML response. This allows the client-side
// store to pick-up the server-side state without having to duplicate
// the initial data fetching on the client.
context
.
initialState
=
store
.
state
return
app
})
...
...
src/views/CreateListView.js
View file @
e7014499
import
ItemList
from
'../components/ItemList.vue'
//
factory function for creating root-level list views
//
This is a factory function for dynamically creating root-level list views,
// since they share most of the logic except for the type of items to display.
// They are essentially higher order components wrapping ItemList.vue.
export
function
createListView
(
type
)
{
return
{
name
:
`
${
type
}
-stories-view`
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment