@@ -19,18 +19,14 @@ var getLocalPath = function (path, context) {
1919/**
2020 * Private method that modifies magics strings to contain their parents
2121 */
22- function addModuleToNames ( name , subapi , instanceName ) {
22+ function addModuleToNames ( name , subapi ) {
2323 var result = { } ;
2424 Object . keys ( subapi ) . forEach ( function ( type ) {
2525 if ( type === 'get' || type === 'act' || type === 'mutate' ) {
2626 result [ type ] = { } ;
2727 Object . keys ( subapi [ type ] ) . forEach ( function ( pathName ) {
2828 var path = subapi [ type ] [ pathName ] ;
29- var subname = path . match ( / [ a - z A - Z ] * / ) [ 0 ] ;
3029 result [ type ] [ pathName ] = name + '/' + path ;
31- if ( instanceName ) {
32- result [ type ] [ pathName ] = result [ type ] [ pathName ] . replace ( subname , subname + '$' + instanceName ) ;
33- }
3430 } ) ;
3531 } else {
3632 result [ type ] = addModuleToNames ( name , subapi [ type ] ) ;
@@ -78,13 +74,12 @@ var storeWrapper = function (store) {
7874 // Clone modules
7975 if ( store . modules ) {
8076 Object . keys ( store . modules ) . forEach ( function ( name ) {
81- var hashPos = name . indexOf ( '$' ) ;
82- var instanceName = hashPos >= 0 ? name . slice ( hashPos + 1 ) : undefined ;
83-
84- store . api [ name ] = addModuleToNames ( camelCasedName , store . modules [ name ] . api , instanceName ) ;
77+ store . api [ name ] = addModuleToNames ( camelCasedName , store . modules [ name ] . api ) ;
8578 } ) ;
8679 }
8780
81+ store . $api = clone ( store . api , false ) ;
82+
8883 return store ;
8984} ;
9085
@@ -241,11 +236,10 @@ var api$1 = {};
241236 */
242237var generateAPI = function ( newImporter ) {
243238 importer$1 = newImporter ;
244-
245239 var modules = importer$1 . getModules ( ) ;
246240 Object . keys ( modules ) . forEach ( function ( module ) {
247241 var camelCasedName = toCamelCase ( modules [ module ] . name ) ;
248- api$1 [ camelCasedName ] = modules [ module ] . api ;
242+ api$1 [ camelCasedName ] = modules [ module ] . $ api;
249243 } ) ;
250244} ;
251245
@@ -271,17 +265,26 @@ function searchDeeper(map, key) {
271265 return result ;
272266}
273267
274- function getFullPath ( config ) {
275- var suffix = config . subinstance ? '$' + config . subinstance : '' ;
276- var getterKey = config . mappedKey . match ( / [ a - z A - Z ] * / ) [ 0 ] ;
277-
268+ var getFullPath = function ( config ) {
269+ var suffix = config . instance ? '$' + config . instance : '' ;
270+ var getterKey = config . subpath . match ( / [ a - z A - Z ] * / ) [ 0 ] ;
278271 var localApi = api$1 [ config . vuexPlus . baseStoreName ] ;
272+
279273 if ( getterKey !== config . vuexPlus . baseStoreName ) {
280274 localApi = searchDeeper ( api$1 [ config . vuexPlus . baseStoreName ] , getterKey + suffix ) ;
281275 }
282- return localApi [ config . method ] [ config . key ]
283- . replace ( config . vuexPlus . baseStoreName , config . vuexPlus . storeInstanceName ) ;
284- }
276+
277+ if ( ! localApi ) {
278+ var instance = config . subpath . split ( '/' ) [ 0 ] + '$' + config . instance ;
279+ console . error ( '[Vuex+ warn]: Cant find substore instance "' + instance + '" in "' + config . container + '"' ) ;
280+ return undefined ;
281+ }
282+
283+ var fullPath = localApi [ config . method ] [ config . key ]
284+ . replace ( config . vuexPlus . baseStoreName , config . vuexPlus . storeInstanceName ) ;
285+
286+ return fullPath ;
287+ } ;
285288
286289var map = {
287290 getters : function getters ( m ) {
@@ -291,12 +294,12 @@ var map = {
291294 var path = getFullPath ( {
292295 method : 'get' ,
293296 key : key ,
294- mappedKey : m [ key ] ,
295- subinstance : this . subinstance ,
297+ subpath : m [ key ] ,
298+ instance : this . instance ,
296299 vuexPlus : this [ '$vuex+' ] ,
300+ container : this . $parent . $vnode . componentOptions . tag ,
297301 } ) ;
298302
299- // localApi.get[key].replace(this['$vuex+'].baseStoreName, this['$vuex+'].storeInstanceName)
300303 return this . $store . getters [ path ] ;
301304 } ;
302305 } ) ;
@@ -310,10 +313,12 @@ var map = {
310313 var path = getFullPath ( {
311314 method : 'act' ,
312315 key : key ,
313- mappedKey : m [ key ] ,
314- subinstance : this . subinstance ,
316+ subpath : m [ key ] ,
317+ instance : this . instance ,
315318 vuexPlus : this [ '$vuex+' ] ,
319+ container : this . $parent . $vnode . componentOptions . tag ,
316320 } ) ;
321+
317322 return this . $store . dispatch ( path , payload ) ;
318323 } ;
319324 } ) ;
@@ -362,13 +367,26 @@ var hmrCallback = hmrHandler;
362367 */
363368var api$$1 = api$1 ;
364369
370+ var newInstance = function newInstance ( substore , instance ) {
371+ var result = clone ( substore ) ;
372+ Object . keys ( result . api ) . forEach ( function ( type ) {
373+ if ( type === 'get' || type === 'act' || type === 'mutate' ) {
374+ Object . keys ( result . api [ type ] ) . forEach ( function ( key ) {
375+ result . api [ type ] [ key ] = result . api [ type ] [ key ] . split ( '/' ) [ 0 ] + '$' + instance + '/' + key ;
376+ } ) ;
377+ }
378+ } ) ;
379+
380+ return result ;
381+ } ;
382+
365383/**
366384 * Method that returns a getter from the same instance.
367385 * @param {string } - Path as as string, usually from api. Eg. `api.example.get.something`
368386 * @param {Context } - Vuex context
369387 * @returns {any } - Value from Vuex getter
370388 */
371- var instance = {
389+ var global = {
372390 get : function get ( ref ) {
373391 var path = ref . path ;
374392 var context = ref . context ;
@@ -403,6 +421,7 @@ var setupDone = false;
403421var vuex_ = {
404422 install : function install ( Vue ) {
405423 Vue . mixin ( {
424+ props : [ 'instance' ] ,
406425 created : function created ( ) {
407426 var this$1 = this ;
408427
@@ -418,11 +437,9 @@ var vuex_ = {
418437
419438 var findModuleName = function ( parent ) {
420439 if ( ! this$1 [ '$vuex+' ] && parent . $parent ) {
421- // console.info(parent.$parent.name, parent.$parent);
422440 if ( ! parent . $parent [ '$vuex+' ] ) {
423441 findModuleName ( parent . $parent , '/' ) ;
424442 } else {
425- // console.info('found [vuex+]', parent.$parent['$vuex+'].baseStoreName);
426443 this$1 [ '$vuex+' ] = {
427444 baseStoreName : parent . $parent [ '$vuex+' ] . baseStoreName ,
428445 storeInstanceName : parent . $parent [ '$vuex+' ] . storeInstanceName ,
@@ -431,11 +448,10 @@ var vuex_ = {
431448 }
432449 } ;
433450
434- // console.info('finding', this.$options['_componentTag']);
435451 findModuleName ( this , '/' ) ;
436452 } ,
437453 } ) ;
438454 } ,
439455} ;
440456
441- export { addStore , map , store , hmrCallback , api$$1 as api , instance } ; export default vuex_ ;
457+ export { addStore , map , store , hmrCallback , api$$1 as api , newInstance , global } ; export default vuex_ ;
0 commit comments