Skip to content

Commit 21a6838

Browse files
committed
compat with latest rc of vuex/vue-router
1 parent e119a00 commit 21a6838

File tree

2 files changed

+25
-55
lines changed

2 files changed

+25
-55
lines changed

index.js

Lines changed: 23 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,44 @@
11
exports.sync = function (store, router) {
2-
patchStore(store)
3-
store.router = router
2+
store.registerModule('route', {
3+
state: {},
4+
mutations: {
5+
'router/ROUTE_CHANGED': function (state, to) {
6+
store.state.route = Object.freeze({
7+
name: to.name,
8+
path: to.path,
9+
hash: to.hash,
10+
query: to.query,
11+
params: to.params,
12+
fullPath: to.fullPath
13+
})
14+
}
15+
}
16+
})
417

5-
var commit = store.commit || store.dispatch
618
var isTimeTraveling = false
719
var currentPath
820

921
// sync router on store change
1022
store.watch(
11-
function (state) {
12-
return state.route
13-
},
23+
function (state) { return state.route },
1424
function (route) {
15-
if (route.path === currentPath) {
25+
if (route.fullPath === currentPath) {
1626
return
1727
}
1828
isTimeTraveling = true
19-
currentPath = route.path
20-
router.go(route.path)
29+
currentPath = route.fullPath
30+
router.push(route)
2131
},
22-
{ deep: true, sync: true }
32+
{ sync: true }
2333
)
2434

2535
// sync store on router navigation
26-
router.afterEach(function (transition) {
36+
router.afterEach(function (to) {
2737
if (isTimeTraveling) {
2838
isTimeTraveling = false
2939
return
3040
}
31-
var to = transition.to
32-
currentPath = to.path
33-
commit('router/ROUTE_CHANGED', to)
34-
})
35-
}
36-
37-
function applyMutationState(store, state) {
38-
// support above 2.0
39-
if (store.hasOwnProperty('_committing')) {
40-
return store._committing = state
41-
}
42-
return store._dispatching = state
43-
}
44-
45-
function patchStore (store) {
46-
// add state
47-
var set = store._vm.constructor.set
48-
applyMutationState(store, true);
49-
set(store.state, 'route', {
50-
path: '',
51-
query: null,
52-
params: null
41+
currentPath = to.fullPath
42+
store.commit('router/ROUTE_CHANGED', to)
5343
})
54-
applyMutationState(store, false);
55-
56-
var routeModule = {
57-
mutations: {
58-
'router/ROUTE_CHANGED': function (state, to) {
59-
store.state.route = to
60-
}
61-
}
62-
}
63-
64-
// add module
65-
if (store.module) {
66-
store.module('route', routeModule)
67-
} else {
68-
store.hotUpdate({
69-
modules: {
70-
route: routeModule
71-
}
72-
})
73-
}
7444
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"homepage": "https://github.com/vuejs/vuex-router-sync#readme",
2121
"peerDependencies": {
22-
"vuex": ">= 0.6.2 < 3",
23-
"vue-router": ">=0.7.11"
22+
"vuex": "^2.0.0-rc.3",
23+
"vue-router": "^2.0.0-rc.1"
2424
}
2525
}

0 commit comments

Comments
 (0)