Skip to content

Commit 164cf31

Browse files
committed
fixed sub modules functionality bug
1 parent c46e923 commit 164cf31

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

src/module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ export function extractVuexModule( cls :typeof VuexModule ) {
5353
// Cache explicit mutations and getter mutations.
5454
VuexClass.prototype.__mutations_cache__.__explicit_mutations__ = fromPrototype.mutations.explicitMutations;
5555
VuexClass.prototype.__mutations_cache__.__setter_mutations__ = fromPrototype.mutations.setterMutations;
56+
const className = VuexClass.name.toLowerCase();
5657

5758
const vuexModule :VuexObject = {
5859
namespaced: VuexClass.prototype.__options__ && VuexClass.prototype.__options__.namespaced ? true : false,
5960
state: fromInstance.state,
6061
mutations: { ...fromPrototype.mutations.explicitMutations, ...fromPrototype.mutations.setterMutations, __internal_mutator__: internalMutator },
61-
getters: { ...fromPrototype.getters, ...fromInstance.getters , __internal_getter__: internalGetter },
62+
getters: { ...fromPrototype.getters, ...fromInstance.getters , [ `__${className}_internal_getter__`]: internalGetter },
6263
actions: { ...fromPrototype.actions, __internal_action__: internalAction },
6364
modules: fromInstance.submodules,
6465
};

src/proxy.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { extractVuexModule } from "./module";
22
import { VuexModuleConstructor, Map, VuexModule, ProxyWatchers } from "./interfaces";
3-
import { getClassPath, toCamelCase } from "./utils";
3+
import { getClassPath, toCamelCase, refineNamespacedPath } from "./utils";
44

55

66
export function clearProxyCache<T extends typeof VuexModule>( cls :T ) {
@@ -45,8 +45,10 @@ export function createProxy<T extends typeof VuexModule>( $store :any, cls :T )
4545
)
4646
}
4747

48+
const className = cls.name.toLowerCase();
49+
4850
return $store.watch(
49-
() => $store.getters[ namespacedPath + "__internal_getter__"]( field ),
51+
() => $store.getters[ namespacedPath + `__${className}_internal_getter__`]( field ),
5052
callback,
5153
options,
5254
)
@@ -232,6 +234,8 @@ function createLocalWatchers( cls :VuexModuleConstructor, $store :Map, namespace
232234

233235
const getterNames = cls.prototype.__explicit_getter_names__;
234236

237+
const className = cls.name.toLowerCase();
238+
235239
for( let field in watchMap ) {
236240

237241
const fieldIsAnExplicitGetter = getterNames.indexOf( field ) > -1;
@@ -252,7 +256,7 @@ function createLocalWatchers( cls :VuexModuleConstructor, $store :Map, namespace
252256
}
253257
else { // This is so we can also watch implicit getters.
254258
$store.watch(
255-
() => $store.getters[ namespacedPath + "__internal_getter__" ]( field ),
259+
() => $store.getters[ namespacedPath + `__${className}_internal_getter__` ]( field ),
256260
proxiedWatchFunc,
257261
)
258262
}
@@ -284,6 +288,9 @@ function createGettersAndMutationProxyFromState({ cls, proxy, state, $store, nam
284288
* and a setter that calls a mutation commit on that value.
285289
* 1.2.2. Go back to STEP 1.
286290
*/
291+
const className = cls.name.toLowerCase();
292+
namespacedPath = refineNamespacedPath( namespacedPath );
293+
287294
for (let field in state) {
288295

289296
let value = state[ field ];
@@ -296,8 +303,9 @@ function createGettersAndMutationProxyFromState({ cls, proxy, state, $store, nam
296303
get: () => {
297304
// When creating local proxies getters doesn't exist on that context, so we have to account
298305
// for that.
299-
if( $store.getters ) return $store.getters[ namespacedPath + "__internal_getter__" ]( path )
300-
else return $store[ "__internal_getter__" ]( path )
306+
if( $store.getters ) {
307+
return $store.getters[ namespacedPath + `__${className}_internal_getter__` ]( path )
308+
}else return $store[ `__${className}_internal_getter__` ]( path )
301309
},
302310
set: payload => {
303311
if( $store.commit ) $store.commit( namespacedPath + "__internal_mutator__", { field: path, payload });
@@ -344,9 +352,10 @@ function createExplicitMutationsProxy( cls :VuexModuleConstructor, proxy :Map, $
344352
function createGettersAndGetterMutationsProxy({ cls, getters, mutations, proxy, $store, namespacedPath } :GetterProxyCreator) {
345353

346354
const getterMutations = Object.keys( cls.prototype.__mutations_cache__.__setter_mutations__ );
355+
const className = cls.name.toLowerCase();
347356
// If there are defined setter mutations that do not have a corresponding getter,
348357
// throw an error.
349-
if( $store && $store.__internal_getter__ ) {
358+
if( $store && $store[`__${className}_internal_getter__`] ) {
350359
$store.__internal_mutator__ = mutations.__internal_mutator__;
351360
}
352361

@@ -384,7 +393,7 @@ function createGettersAndGetterMutationsProxy({ cls, getters, mutations, proxy,
384393
function createActionProxy({ cls, actions, proxy, $store, namespacedPath } :ActionProxyCreator) {
385394

386395
const dispatch = cls.prototype.__store_cache__ ? cls.prototype.__store_cache__.dispatch : $store.dispatch;
387-
namespacedPath = cls.prototype.__namespacedPath__.length ? cls.prototype.__namespacedPath__ + "/" : namespacedPath;
396+
namespacedPath = refineNamespacedPath( cls.prototype.__namespacedPath__.length ? cls.prototype.__namespacedPath__ + "/" : namespacedPath );
388397

389398
for( let field in actions ) {
390399
proxy[ field ] = function( payload :any ) {

src/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ export function getClassPath( path :string ) {
66
const arr = path.split( "/" );
77
return arr[ arr.length - 1 ];
88
}
9+
10+
export function refineNamespacedPath( path :string ) {
11+
const rtn = path.split( "/" ).filter( str => str.trim().length > 0 ).join( "/" ).trim();
12+
console.log( "Return", rtn );
13+
if( rtn.length > 0 ) return rtn + "/"
14+
else return rtn;
15+
}

test/extract-vuex-module.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('ExtractVuexModule', () => {
103103
const { getters } = UserStore.ExtractVuexModule(UserStore)
104104
// Note all states are automatically accessible as getters.
105105
// This makes th `@getter` decorator redundant. But we have it for backwards compatibility.
106-
expect(Object.keys(getters)).toEqual([ 'fullName', '__internal_getter__' ])
106+
expect(Object.keys(getters)).toEqual([ 'fullName', `__${UserStore.name.toLowerCase()}_internal_getter__` ])
107107
})
108108

109109
it('should extract all actions', () => {

0 commit comments

Comments
 (0)