1
- import { extractVuexModule } from "./module" ;
1
+ import { extractVuexModule , getNamespacedPath } from "./module" ;
2
2
import { VuexModuleConstructor , Map , VuexModule , ProxyWatchers } from "./interfaces" ;
3
3
import { getClassPath , toCamelCase , refineNamespacedPath } from "./utils" ;
4
4
@@ -36,7 +36,7 @@ export function createProxy<T extends typeof VuexModule>( $store :any, cls :T )
36
36
// If field is a getter use the normal getter path if not use internal getters.
37
37
if ( typeof field === "string" && getterNames . indexOf ( field ) > - 1 ) {
38
38
return $store . watch (
39
- ( ) => $store . getters [ namespacedPath + field ] ,
39
+ ( ) => ( $store . rootGetters || $store . getters ) [ namespacedPath + field ] ,
40
40
callback ,
41
41
options ,
42
42
)
@@ -45,7 +45,7 @@ export function createProxy<T extends typeof VuexModule>( $store :any, cls :T )
45
45
const className = cls . name . toLowerCase ( ) ;
46
46
47
47
return $store . watch (
48
- ( ) => $store . getters [ namespacedPath + `__${ className } _internal_getter__` ] ( field ) ,
48
+ ( ) => ( $store . rootGetters || $store . getters ) [ namespacedPath + `__${ className } _internal_getter__` ] ( field ) ,
49
49
callback ,
50
50
options ,
51
51
)
@@ -251,13 +251,13 @@ function createLocalWatchers( cls :VuexModuleConstructor, $store :Map, namespace
251
251
252
252
if ( fieldIsAnExplicitGetter ) {
253
253
$store . watch (
254
- ( ) => $store . getters [ namespacedPath + field ] ,
254
+ ( ) => ( $store . rootGetters || $store . getters ) [ namespacedPath + field ] ,
255
255
proxiedWatchFunc ,
256
256
)
257
257
}
258
258
else { // This is so we can also watch implicit getters.
259
259
$store . watch (
260
- ( ) => $store . getters [ namespacedPath + `__${ className } _internal_getter__` ] ( field ) ,
260
+ ( ) => ( $store . rootGetters || $store . getters ) [ namespacedPath + `__${ className } _internal_getter__` ] ( field ) ,
261
261
proxiedWatchFunc ,
262
262
)
263
263
}
@@ -270,7 +270,8 @@ function createSubModuleProxy( $store :Map, cls:VuexModuleConstructor, proxy :Ma
270
270
const store = cls . prototype . __store_cache__ || $store ;
271
271
for ( let field in modules ) {
272
272
const subModuleClass = cls . prototype . __submodules_cache__ [ field ] as VuexModuleConstructor ;
273
- subModuleClass . prototype . __namespacedPath__ = cls . prototype . __namespacedPath__ + "/" + subModuleClass . prototype . __namespacedPath__ ;
273
+ const namespacedPath = getNamespacedPath ( subModuleClass ) ;
274
+ subModuleClass . prototype . __namespacedPath__ = cls . prototype . __namespacedPath__ + "/" + namespacedPath ;
274
275
proxy [ field ] = createProxy ( store , subModuleClass ) ;
275
276
}
276
277
@@ -310,17 +311,19 @@ function createGettersAndMutationProxyFromState({ cls, proxy, state, $store, nam
310
311
get : ( ) => {
311
312
// When creating local proxies getters doesn't exist on that context, so we have to account
312
313
// for that.
313
- if ( $store . getters ) {
314
- return $store . getters [ namespacedPath + `__${ className } _internal_getter__` ] ( path )
314
+ const getters = $store . rootGetters || $store . getters ;
315
+ if ( getters ) {
316
+ const getterPath = refineNamespacedPath ( cls . prototype . __namespacedPath__ ) + `__${ className } _internal_getter__` ;
317
+ return getters [ getterPath ] ( path )
315
318
} else return $store [ `__${ className } _internal_getter__` ] ( path )
316
319
} ,
317
320
set : payload => {
318
321
const commit = $store . commit || cls . prototype . __store_cache__ . commit ;
319
- if ( commit ) commit ( refineNamespacedPath ( cls . prototype . __namespacedPath__ ) + `__${ className } _internal_mutator__` , { field : path , payload } ) ;
322
+ if ( commit ) commit ( refineNamespacedPath ( cls . prototype . __namespacedPath__ ) + `__${ className } _internal_mutator__` , { field : path , payload } , { root : true } ) ;
320
323
else {
321
324
// We must be creating local proxies hence, $store.commit doesn't exist
322
325
const store = cls . prototype . __context_store__ ! ;
323
- store . commit ( `__${ className } _internal_mutator__` , { field : path , payload } )
326
+ store . commit ( `__${ className } _internal_mutator__` , { field : path , payload } , { root : true } )
324
327
}
325
328
} ,
326
329
} )
@@ -401,7 +404,7 @@ function __createGettersAndMutationProxyFromState({ cls, proxy, state, $store, n
401
404
else {
402
405
// We must be creating local proxies hence, $store.commit doesn't exist
403
406
const store = cls . prototype . __context_store__ ! ;
404
- store . commit ( `__${ className } _internal_mutator__` , { field, payload } )
407
+ store . commit ( `__${ className } _internal_mutator__` , { field, payload } , { root : true } )
405
408
}
406
409
} ,
407
410
} )
@@ -437,7 +440,7 @@ function createExplicitMutationsProxy( cls :VuexModuleConstructor, proxy :Map, $
437
440
) ;
438
441
439
442
for ( let field in mutations ) {
440
- proxy [ field ] = ( payload :any ) => commit ( namespacedPath + field , payload )
443
+ proxy [ field ] = ( payload :any ) => commit ( namespacedPath + field , payload , { root : true } )
441
444
}
442
445
443
446
}
@@ -465,21 +468,23 @@ function createGettersAndGetterMutationsProxy({ cls, getters, mutations, proxy,
465
468
466
469
Object . defineProperty ( proxy , field , {
467
470
get : ( ) => {
468
- if ( $store . getters ) return $store . getters [ namespacedPath + field ]
471
+ const storeGetters = namespacedPath ? $store . rootGetters : $store . getters ;
472
+ if ( storeGetters ) return storeGetters [ namespacedPath + field ]
469
473
else return $store [ namespacedPath + field ] ;
470
474
} ,
471
- set : ( payload :any ) => $store . commit ( namespacedPath + field , payload ) ,
475
+ set : ( payload :any ) => $store . commit ( namespacedPath + field , payload , { root : ! ! namespacedPath } ) ,
472
476
} )
473
477
474
478
continue ;
475
479
}
476
480
477
481
// The field has only a getter.
478
- if ( proxy [ field ] ) continue ;
482
+ if ( Object . prototype . hasOwnProperty . call ( proxy , field ) ) continue ;
479
483
480
484
Object . defineProperty ( proxy , field , {
481
485
get : ( ) => {
482
- if ( $store . getters ) return $store . getters [ namespacedPath + field ] ;
486
+ const storeGetters = $store . rootGetters || $store . getters ;
487
+ if ( storeGetters ) return storeGetters [ namespacedPath + field ] ;
483
488
else return $store [ namespacedPath + field ] ;
484
489
}
485
490
} )
0 commit comments