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
8db2ee92
Commit
8db2ee92
authored
Aug 09, 2016
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
realtime updates
parent
55122cae
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
3 deletions
+40
-3
NewsList.vue
src/components/NewsList.vue
+14
-1
api.js
src/store/api.js
+16
-0
index.js
src/store/index.js
+10
-2
No files found.
src/components/NewsList.vue
View file @
8db2ee92
...
@@ -27,6 +27,7 @@
...
@@ -27,6 +27,7 @@
<
script
>
<
script
>
import
Spinner
from
'./Spinner.vue'
import
Spinner
from
'./Spinner.vue'
import
NewsItem
from
'./NewsItem.vue'
import
NewsItem
from
'./NewsItem.vue'
import
{
watchList
}
from
'../store/api'
export
default
{
export
default
{
name
:
'NewsList'
,
name
:
'NewsList'
,
...
@@ -46,7 +47,9 @@ export default {
...
@@ -46,7 +47,9 @@ export default {
loading
:
false
,
loading
:
false
,
transition
:
'slide-left'
,
transition
:
'slide-left'
,
// if this is the initial render, directly render with the store state
// if this is the initial render, directly render with the store state
// otherwise this is a page switch, start with blank and wait for data load
// otherwise this is a page switch, start with blank and wait for data load.
// we need these local state so that we can precisely control the timing
// of the transitions.
displayedPage
:
isInitialRender
?
Number
(
this
.
$store
.
state
.
route
.
params
.
page
)
||
1
:
-
1
,
displayedPage
:
isInitialRender
?
Number
(
this
.
$store
.
state
.
route
.
params
.
page
)
||
1
:
-
1
,
displayedItems
:
isInitialRender
?
this
.
$store
.
getters
.
activeItems
:
[]
displayedItems
:
isInitialRender
?
this
.
$store
.
getters
.
activeItems
:
[]
}
}
...
@@ -69,6 +72,16 @@ export default {
...
@@ -69,6 +72,16 @@ export default {
if
(
this
.
$root
.
_isMounted
)
{
if
(
this
.
$root
.
_isMounted
)
{
this
.
loadItems
(
this
.
page
)
this
.
loadItems
(
this
.
page
)
}
}
this
.
unwatchList
=
watchList
(
this
.
type
,
ids
=>
{
this
.
$store
.
commit
(
'SET_LIST'
,
{
type
:
this
.
type
,
ids
})
this
.
$store
.
dispatch
(
'FETCH_ACTIVE_ITEMS'
).
then
(()
=>
{
this
.
displayedItems
=
this
.
$store
.
getters
.
activeItems
})
})
},
destroyed
()
{
this
.
unwatchList
()
},
},
watch
:
{
watch
:
{
...
...
src/store/api.js
View file @
8db2ee92
...
@@ -71,3 +71,19 @@ export function fetchItem (id, forceRefresh) {
...
@@ -71,3 +71,19 @@ export function fetchItem (id, forceRefresh) {
export
function
fetchItems
(
ids
)
{
export
function
fetchItems
(
ids
)
{
return
Promise
.
all
(
ids
.
map
(
id
=>
fetchItem
(
id
)))
return
Promise
.
all
(
ids
.
map
(
id
=>
fetchItem
(
id
)))
}
}
export
function
watchList
(
type
,
cb
)
{
let
first
=
true
const
ref
=
api
.
child
(
`
${
type
}
stories`
)
const
handler
=
snapshot
=>
{
if
(
first
)
{
first
=
false
}
else
{
cb
(
snapshot
.
val
())
}
}
ref
.
on
(
'value'
,
handler
)
return
()
=>
{
ref
.
off
(
'value'
,
handler
)
}
}
src/store/index.js
View file @
8db2ee92
...
@@ -26,8 +26,16 @@ const store = new Vuex.Store({
...
@@ -26,8 +26,16 @@ const store = new Vuex.Store({
commit
(
'SET_ACTIVE_TYPE'
,
{
type
})
commit
(
'SET_ACTIVE_TYPE'
,
{
type
})
return
fetchIdsByType
(
type
)
return
fetchIdsByType
(
type
)
.
then
(
ids
=>
commit
(
'SET_LIST'
,
{
type
,
ids
}))
.
then
(
ids
=>
commit
(
'SET_LIST'
,
{
type
,
ids
}))
.
then
(()
=>
fetchItems
(
getters
.
activeIds
.
filter
(
id
=>
!
state
.
items
[
id
])))
.
then
(()
=>
dispatch
(
'FETCH_ACTIVE_ITEMS'
))
.
then
(
items
=>
commit
(
'SET_ITEMS'
,
{
items
}))
},
FETCH_ACTIVE_ITEMS
:
({
commit
,
state
,
getters
})
=>
{
const
ids
=
getters
.
activeIds
.
filter
(
id
=>
!
state
.
items
[
id
])
if
(
ids
.
length
)
{
return
fetchItems
(
ids
).
then
(
items
=>
commit
(
'SET_ITEMS'
,
{
items
}))
}
else
{
return
Promise
.
resolve
()
}
}
}
},
},
...
...
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