File tree Expand file tree Collapse file tree 5 files changed +36
-30
lines changed Expand file tree Collapse file tree 5 files changed +36
-30
lines changed Original file line number Diff line number Diff line change 8
8
:message =" message" >
9
9
</message >
10
10
</ul >
11
- <textarea class =" message-composer" @keyup.enter =" sendMessage " ></textarea >
11
+ <textarea class =" message-composer" @keyup.enter =" trySendMessage " ></textarea >
12
12
</div >
13
13
</template >
14
14
15
15
<script >
16
- import store from ' ../store'
17
16
import Message from ' ./Message.vue'
17
+ import { sendMessage } from ' ../store/actions'
18
18
19
19
export default {
20
20
components: { Message },
21
- computed: {
22
- thread () {
23
- const id = store .state .currentThreadID
24
- return id
25
- ? store .state .threads [id]
26
- : {}
21
+ vuex: {
22
+ state: {
23
+ thread ({ currentThreadID, threads }) {
24
+ return currentThreadID ? threads[currentThreadID] : {}
25
+ },
26
+ messages ({ messages }) {
27
+ const messageIds = this .thread .messages
28
+ return messageIds && messageIds .map (id => messages[id])
29
+ }
27
30
},
28
- messages () {
29
- return this .thread .messages &&
30
- this .thread .messages .map (id => store .state .messages [id])
31
+ actions: {
32
+ sendMessage
31
33
}
32
34
},
33
35
watch: {
@@ -39,10 +41,10 @@ export default {
39
41
}
40
42
},
41
43
methods: {
42
- sendMessage (e ) {
44
+ trySendMessage (e ) {
43
45
const text = e .target .value
44
46
if (text .trim ()) {
45
- store . actions .sendMessage (text, this .thread )
47
+ this .sendMessage (text, this .thread )
46
48
e .target .value = ' '
47
49
}
48
50
}
Original file line number Diff line number Diff line change 2
2
<li
3
3
class =" thread-list-item"
4
4
:class =" { active: isCurrentThread }"
5
- @click =" onClick " >
5
+ @click =" switchThread(thread.id) " >
6
6
<h5 class =" thread-name" >{{ thread.name }}</h5 >
7
7
<div class =" thread-time" >
8
8
{{ thread.lastMessage.timestamp | time }}
14
14
</template >
15
15
16
16
<script >
17
- import store from ' ../store'
17
+ import { switchThread } from ' ../store/actions '
18
18
19
19
export default {
20
20
props: [' thread' ],
21
- computed : {
22
- isCurrentThread () {
23
- return this . thread . id === store . state . currentThreadID
24
- }
25
- },
26
- methods : {
27
- onClick () {
28
- store . actions . switchThread ( this . thread . id )
21
+ vuex : {
22
+ state : {
23
+ isCurrentThread ({ currentThreadID }) {
24
+ return this . thread . id === currentThreadID
25
+ }
26
+ },
27
+ actions : {
28
+ switchThread
29
29
}
30
30
}
31
31
}
Original file line number Diff line number Diff line change @@ -21,12 +21,14 @@ import Thread from './Thread.vue'
21
21
22
22
export default {
23
23
components: { Thread },
24
+ vuex: {
25
+ state: {
26
+ threads : state => state .threads
27
+ }
28
+ },
24
29
computed: {
25
- threads () {
26
- return store .state .threads
27
- },
28
30
unreadCount () {
29
- const threads = store . state .threads
31
+ const threads = this .threads
30
32
return Object .keys (threads).reduce ((count , id ) => {
31
33
return threads[id].lastMessage .isRead
32
34
? count
Original file line number Diff line number Diff line change @@ -2,14 +2,18 @@ import 'babel-polyfill'
2
2
import Vue from 'vue'
3
3
import App from './components/App.vue'
4
4
import store from './store'
5
+ import { getAllMessages } from './store/actions'
6
+
7
+ Vue . config . debug = true
5
8
6
9
Vue . filter ( 'time' , timestamp => {
7
10
return new Date ( timestamp ) . toLocaleTimeString ( )
8
11
} )
9
12
10
13
new Vue ( {
11
14
el : 'body' ,
15
+ store,
12
16
components : { App }
13
17
} )
14
18
15
- store . actions . getAllMessages ( )
19
+ getAllMessages ( store )
Original file line number Diff line number Diff line change 1
1
import Vue from 'vue'
2
2
import Vuex from '../../../src'
3
- import * as actions from './actions'
4
3
import mutations from './mutations'
5
4
6
5
Vue . use ( Vuex )
@@ -32,7 +31,6 @@ export default new Vuex.Store({
32
31
*/
33
32
}
34
33
} ,
35
- actions,
36
34
mutations,
37
35
middlewares : process . env . NODE_ENV !== 'production'
38
36
? [ Vuex . createLogger ( ) ]
You can’t perform that action at this time.
0 commit comments