1
- // export install function
2
1
export default function ( Vue ) {
2
+ // override init and inject vuex init procedure
3
3
const _init = Vue . prototype . _init
4
- Vue . prototype . _init = function ( options ) {
5
- options = options || { }
6
- const componentOptions = this . constructor . options
4
+ Vue . prototype . _init = function ( options = { } ) {
5
+ options . init = options . init
6
+ ? [ vuexInit ] . concat ( options . init )
7
+ : vuexInit
8
+ _init . call ( this , options )
9
+ }
10
+
11
+ function vuexInit ( ) {
12
+ const options = this . $options
13
+ const { store, vuex } = options
7
14
// store injection
8
- const store = options . store || componentOptions . store
9
15
if ( store ) {
10
16
this . $store = store
11
17
} else if ( options . parent && options . parent . $store ) {
12
18
this . $store = options . parent . $store
13
19
}
14
20
// vuex option handling
15
- const vuex = options . vuex || componentOptions . vuex
16
21
if ( vuex ) {
17
22
if ( ! this . $store ) {
18
23
console . warn (
@@ -21,32 +26,52 @@ export default function (Vue) {
21
26
)
22
27
}
23
28
let { state, getters, actions } = vuex
24
- // getters
29
+ // handle deprecated state option
25
30
if ( state && ! getters ) {
26
31
console . warn (
27
- '[vuex] vuex.state option has been deprecated. ' +
32
+ '[vuex] vuex.state option will been deprecated in 1.0 . ' +
28
33
'Use vuex.getters instead.'
29
34
)
30
35
getters = state
31
36
}
37
+ // getters
32
38
if ( getters ) {
33
39
options . computed = options . computed || { }
34
- Object . keys ( getters ) . forEach ( key => {
35
- options . computed [ key ] = function vuexBoundGetter ( ) {
36
- return getters [ key ] . call ( this , this . $store . state )
37
- }
38
- } )
40
+ for ( let key in getters ) {
41
+ options . computed [ key ] = makeBoundGetter ( getters [ key ] )
42
+ }
39
43
}
40
44
// actions
41
45
if ( actions ) {
42
46
options . methods = options . methods || { }
43
- Object . keys ( actions ) . forEach ( key => {
44
- options . methods [ key ] = function vuexBoundAction ( ...args ) {
45
- return actions [ key ] . call ( this , this . $store , ...args )
46
- }
47
- } )
47
+ for ( let key in actions ) {
48
+ options . methods [ key ] = makeBoundAction ( actions [ key ] )
49
+ }
48
50
}
49
51
}
50
- _init . call ( this , options )
52
+ }
53
+
54
+ function makeBoundGetter ( getter ) {
55
+ return function vuexBoundGetter ( ) {
56
+ return getter . call ( this , this . $store . state )
57
+ }
58
+ }
59
+
60
+ function makeBoundAction ( action ) {
61
+ return function vuexBoundAction ( ...args ) {
62
+ return action . call ( this , this . $store , ...args )
63
+ }
64
+ }
65
+
66
+ // option merging
67
+ const merge = Vue . config . optionMergeStrategies . computed
68
+ Vue . config . optionMergeStrategies . vuex = ( toVal , fromVal ) => {
69
+ if ( ! toVal ) return fromVal
70
+ if ( ! fromVal ) return toVal
71
+ return {
72
+ getters : merge ( toVal . getters , fromVal . getters ) ,
73
+ state : merge ( toVal . state , fromVal . state ) ,
74
+ actions : merge ( toVal . actions , fromVal . actions )
75
+ }
51
76
}
52
77
}
0 commit comments