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
e9257b85
Commit
e9257b85
authored
Aug 09, 2016
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
comments
parent
8db2ee92
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
11 additions
and
0 deletions
+11
-0
NewsList.vue
src/components/NewsList.vue
+1
-0
api.js
src/store/api.js
+3
-0
index.js
src/store/index.js
+7
-0
No files found.
src/components/NewsList.vue
View file @
e9257b85
...
@@ -72,6 +72,7 @@ export default {
...
@@ -72,6 +72,7 @@ export default {
if
(
this
.
$root
.
_isMounted
)
{
if
(
this
.
$root
.
_isMounted
)
{
this
.
loadItems
(
this
.
page
)
this
.
loadItems
(
this
.
page
)
}
}
// watch the current list for realtime updates
this
.
unwatchList
=
watchList
(
this
.
type
,
ids
=>
{
this
.
unwatchList
=
watchList
(
this
.
type
,
ids
=>
{
this
.
$store
.
commit
(
'SET_LIST'
,
{
type
:
this
.
type
,
ids
})
this
.
$store
.
commit
(
'SET_LIST'
,
{
type
:
this
.
type
,
ids
})
this
.
$store
.
dispatch
(
'FETCH_ACTIVE_ITEMS'
).
then
(()
=>
{
this
.
$store
.
dispatch
(
'FETCH_ACTIVE_ITEMS'
).
then
(()
=>
{
...
...
src/store/api.js
View file @
e9257b85
...
@@ -79,11 +79,14 @@ export function watchList (type, cb) {
...
@@ -79,11 +79,14 @@ export function watchList (type, cb) {
if
(
first
)
{
if
(
first
)
{
first
=
false
first
=
false
}
else
{
}
else
{
console
.
log
(
`update for
${
type
}
`
)
cb
(
snapshot
.
val
())
cb
(
snapshot
.
val
())
}
}
}
}
console
.
log
(
`watching
${
type
}
...`
)
ref
.
on
(
'value'
,
handler
)
ref
.
on
(
'value'
,
handler
)
return
()
=>
{
return
()
=>
{
console
.
log
(
`stop watching
${
type
}
`
)
ref
.
off
(
'value'
,
handler
)
ref
.
off
(
'value'
,
handler
)
}
}
}
}
src/store/index.js
View file @
e9257b85
...
@@ -22,6 +22,7 @@ const store = new Vuex.Store({
...
@@ -22,6 +22,7 @@ const store = new Vuex.Store({
},
},
actions
:
{
actions
:
{
// ensure data for rendering given list type
FETCH_DATA_FOR_TYPE
:
({
commit
,
dispatch
,
state
,
getters
},
{
type
})
=>
{
FETCH_DATA_FOR_TYPE
:
({
commit
,
dispatch
,
state
,
getters
},
{
type
})
=>
{
commit
(
'SET_ACTIVE_TYPE'
,
{
type
})
commit
(
'SET_ACTIVE_TYPE'
,
{
type
})
return
fetchIdsByType
(
type
)
return
fetchIdsByType
(
type
)
...
@@ -29,7 +30,9 @@ const store = new Vuex.Store({
...
@@ -29,7 +30,9 @@ const store = new Vuex.Store({
.
then
(()
=>
dispatch
(
'FETCH_ACTIVE_ITEMS'
))
.
then
(()
=>
dispatch
(
'FETCH_ACTIVE_ITEMS'
))
},
},
// ensure all active items are fetched
FETCH_ACTIVE_ITEMS
:
({
commit
,
state
,
getters
})
=>
{
FETCH_ACTIVE_ITEMS
:
({
commit
,
state
,
getters
})
=>
{
// only fetch items that we don't already have.
const
ids
=
getters
.
activeIds
.
filter
(
id
=>
!
state
.
items
[
id
])
const
ids
=
getters
.
activeIds
.
filter
(
id
=>
!
state
.
items
[
id
])
if
(
ids
.
length
)
{
if
(
ids
.
length
)
{
return
fetchItems
(
ids
).
then
(
items
=>
commit
(
'SET_ITEMS'
,
{
items
}))
return
fetchItems
(
ids
).
then
(
items
=>
commit
(
'SET_ITEMS'
,
{
items
}))
...
@@ -58,6 +61,8 @@ const store = new Vuex.Store({
...
@@ -58,6 +61,8 @@ const store = new Vuex.Store({
},
},
getters
:
{
getters
:
{
// ids of the items that should be currently displayed based on
// current list type and current pagination
activeIds
(
state
)
{
activeIds
(
state
)
{
const
{
activeType
,
itemsPerPage
,
lists
}
=
state
const
{
activeType
,
itemsPerPage
,
lists
}
=
state
const
page
=
Number
(
state
.
route
.
params
.
page
)
||
1
const
page
=
Number
(
state
.
route
.
params
.
page
)
||
1
...
@@ -70,6 +75,8 @@ const store = new Vuex.Store({
...
@@ -70,6 +75,8 @@ const store = new Vuex.Store({
}
}
},
},
// items that should be currently displayed.
// this Array may not be fully fetched.
activeItems
(
state
,
getters
)
{
activeItems
(
state
,
getters
)
{
return
getters
.
activeIds
.
map
(
id
=>
state
.
items
[
id
]).
filter
(
_
=>
_
)
return
getters
.
activeIds
.
map
(
id
=>
state
.
items
[
id
]).
filter
(
_
=>
_
)
}
}
...
...
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