Skip to content

Commit

Permalink
compat with latest rc of vuex/vue-router
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 5, 2016
1 parent e119a00 commit 21a6838
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 55 deletions.
76 changes: 23 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,44 @@
exports.sync = function (store, router) {
patchStore(store)
store.router = router
store.registerModule('route', {
state: {},
mutations: {
'router/ROUTE_CHANGED': function (state, to) {
store.state.route = Object.freeze({
name: to.name,
path: to.path,
hash: to.hash,
query: to.query,
params: to.params,
fullPath: to.fullPath
})
}
}
})

var commit = store.commit || store.dispatch
var isTimeTraveling = false
var currentPath

// sync router on store change
store.watch(
function (state) {
return state.route
},
function (state) { return state.route },
function (route) {
if (route.path === currentPath) {
if (route.fullPath === currentPath) {
return
}
isTimeTraveling = true
currentPath = route.path
router.go(route.path)
currentPath = route.fullPath
router.push(route)
},
{ deep: true, sync: true }
{ sync: true }
)

// sync store on router navigation
router.afterEach(function (transition) {
router.afterEach(function (to) {
if (isTimeTraveling) {
isTimeTraveling = false
return
}
var to = transition.to
currentPath = to.path
commit('router/ROUTE_CHANGED', to)
})
}

function applyMutationState(store, state) {
// support above 2.0
if (store.hasOwnProperty('_committing')) {
return store._committing = state
}
return store._dispatching = state
}

function patchStore (store) {
// add state
var set = store._vm.constructor.set
applyMutationState(store, true);
set(store.state, 'route', {
path: '',
query: null,
params: null
currentPath = to.fullPath
store.commit('router/ROUTE_CHANGED', to)
})
applyMutationState(store, false);

var routeModule = {
mutations: {
'router/ROUTE_CHANGED': function (state, to) {
store.state.route = to
}
}
}

// add module
if (store.module) {
store.module('route', routeModule)
} else {
store.hotUpdate({
modules: {
route: routeModule
}
})
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"homepage": "https://github.com/vuejs/vuex-router-sync#readme",
"peerDependencies": {
"vuex": ">= 0.6.2 < 3",
"vue-router": ">=0.7.11"
"vuex": "^2.0.0-rc.3",
"vue-router": "^2.0.0-rc.1"
}
}

0 comments on commit 21a6838

Please sign in to comment.