Skip to content

Commit f2ca657

Browse files
committed
before/after hooks
1 parent e732af4 commit f2ca657

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

example/example.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ var VueRouter = require('../src')
44
Vue.use(VueRouter)
55

66
var router = new VueRouter({
7-
pushstate: true,
7+
// pushstate: true,
88
root: '/hello'
99
})
1010

1111
var root = new Vue({
12-
el: '#app',
1312
components: {
1413
inbox: {
1514
template: '<div><h2>inbox!</h2><router-view></router-view>',
@@ -51,22 +50,30 @@ var root = new Vue({
5150

5251
router.map({
5352
'/inbox': {
54-
name: 'inbox',
5553
component: 'inbox',
54+
before: function (to, from) {
55+
console.log('before')
56+
console.log(to.path, from && from.path)
57+
if (from && from.path === '/about') {
58+
alert('not allowed')
59+
return false
60+
}
61+
},
62+
after: function (to, from) {
63+
console.log('after')
64+
console.log(to.path, from && from.path)
65+
},
5666
subRoutes: {
5767
'/message/:messageId': {
58-
name: 'message',
5968
component: 'message',
6069
alwaysRefresh: true
6170
},
6271
'/archived': {
63-
name: 'archive',
6472
component: 'archive'
6573
}
6674
}
6775
},
6876
'/user/:userId': {
69-
name: 'user',
7077
component: 'user',
7178
subRoutes: {
7279
'profile/:something': {
@@ -92,4 +99,5 @@ router.redirect({
9299
'/info': '/about'
93100
})
94101

95-
router.start(root)
102+
router.start(root)
103+
root.$mount('#app')

src/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var Recognizer = require('route-recognizer')
2-
var hasPushState = history && history.pushState
2+
var hasPushState = typeof history !== 'undefined' && history.pushState
33

44
/**
55
* Router constructor
@@ -17,6 +17,7 @@ function VueRouter (options) {
1717
this._currentPath = null
1818
this._notfoundHandler = null
1919
this._root = null
20+
this._hasPushState = hasPushState
2021
var root = options && options.root
2122
if (root) {
2223
// make sure there's the starting slash
@@ -94,15 +95,20 @@ p.redirect = function (map) {
9495
* be resolved against root (if provided)
9596
*
9697
* @param {String} path
98+
* @param {Object} [options]
9799
*/
98100

99-
p.go = function (path) {
101+
p.go = function (path, options) {
100102
if (this._pushstate) {
101103
// make it relative to root
102104
path = this._root
103105
? this._root + '/' + path.replace(/^\//, '')
104106
: path
105-
history.pushState({}, '', path)
107+
if (options && options.replace) {
108+
history.replaceState({}, '', path)
109+
} else {
110+
history.pushState({}, '', path)
111+
}
106112
this._match(path)
107113
} else {
108114
path = path.replace(/^#!?/, '')

src/view.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,48 @@ module.exports = function (Vue) {
3333
},
3434

3535
onRouteChange: function (route) {
36+
var previousRoute = this.currentRoute
3637
this.currentRoute = route
38+
3739
if (!route._matched) {
3840
// route not found, this outlet is invalidated
3941
return this.invalidate()
4042
}
43+
4144
var segment = route._matched[route._matchedCount]
4245
if (!segment) {
4346
// no segment that matches this outlet
4447
return this.invalidate()
4548
}
49+
4650
// mutate the route as we pass it further down the
4751
// chain. this series of mutation is done exactly once
4852
// for every route as we match the components to render.
4953
route._matchedCount++
5054
// trigger component switch
51-
if (segment.handler.component !== this.currentComponentId ||
52-
segment.handler.alwaysRefresh) {
53-
// TODO: handle before/after hooks
54-
this.currentComponentId = segment.handler.component
55-
this.update(segment.handler.component)
55+
var handler = segment.handler
56+
if (handler.component !== this.currentComponentId ||
57+
handler.alwaysRefresh) {
58+
// call before hook
59+
if (handler.before) {
60+
var beforeResult = handler.before(route, previousRoute)
61+
if (beforeResult === false) {
62+
if (route._router._hasPushState) {
63+
history.back()
64+
} else if (previousRoute) {
65+
route._router.go(previousRoute.path)
66+
}
67+
return
68+
}
69+
}
70+
this.currentComponentId = handler.component
71+
// actually switch component
72+
this.realUpdate(handler.component, function () {
73+
// call after hook
74+
if (handler.after) {
75+
handler.after(route, previousRoute)
76+
}
77+
})
5678
} else if (this.childVM) {
5779
// update route context
5880
this.childVM.route = route
@@ -61,7 +83,7 @@ module.exports = function (Vue) {
6183

6284
invalidate: function () {
6385
this.currentComponentId = null
64-
this.update(null)
86+
this.realUpdate(null)
6587
},
6688

6789
// currently duplicating some logic from v-component
@@ -96,6 +118,7 @@ module.exports = function (Vue) {
96118

97119
unbind: function () {
98120
this.unwatch()
121+
component.unbind.call(this)
99122
}
100123

101124
})

0 commit comments

Comments
 (0)