Skip to content

Commit 1153cc8

Browse files
committed
make component instance available inside activate hook
1 parent 05561fd commit 1153cc8

File tree

3 files changed

+62
-44
lines changed

3 files changed

+62
-44
lines changed

src/directives/view.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module.exports = function (Vue) {
22

33
var _ = Vue.util
44
var util = require('../util')
5-
var pipeline = require('../pipeline')
65
var componentDef = Vue.directive('_component')
76

87
// <router-view> extends the internal component directive
@@ -23,12 +22,6 @@ module.exports = function (Vue) {
2322
)
2423
return
2524
}
26-
// all we need to do here is registering this view
27-
// in the router. actual component switching will be
28-
// managed by the pipeline.
29-
var router = this.router = route._router
30-
this.depth = router._views.length
31-
router._views.unshift(this)
3225
// force dynamic directive so v-component doesn't
3326
// attempt to build right now
3427
this._isDynamicLiteral = true
@@ -40,10 +33,22 @@ module.exports = function (Vue) {
4033
this.keepAlive = false
4134
util.warn('<router-view> does not support keep-alive.')
4235
}
43-
// only activate on create if this is not the
44-
// initial render.
45-
if (router.app) {
46-
pipeline.activate(this, router._currentTransition)
36+
37+
// all we need to do here is registering this view
38+
// in the router. actual component switching will be
39+
// managed by the pipeline.
40+
var router = this.router = route._router
41+
this.depth = router._views.length
42+
router._views.unshift(this)
43+
44+
// note the views are in reverse order.
45+
var parentView = router._views[1]
46+
if (parentView) {
47+
// register self as a child of the parent view,
48+
// instead of activating now. This is so that the
49+
// child's activate hook is called after the
50+
// parent's has resolved.
51+
parentView.childView = this
4752
}
4853
},
4954

src/pipeline.js

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,31 @@ exports.activate = function (view, transition, cb) {
103103
return
104104
}
105105

106-
var Component = handler.component
106+
var Component = view.Component = handler.component
107107
var activateHook = util.getRouteConfig(Component, 'activate')
108108
var dataHook = util.getRouteConfig(Component, 'data')
109109
var waitForData = util.getRouteConfig(Component, 'waitForData')
110110

111-
var build = function (data) {
112-
view.unbuild(true)
113-
view.Component = Component
114-
var shouldLoadData = dataHook && !waitForData
115-
var component = view.build({
116-
data: data,
117-
_meta: {
118-
$loadingRouteData: shouldLoadData
119-
}
120-
})
121-
if (shouldLoadData) {
122-
loadData(component, transition, dataHook)
111+
// unbuild current component. this step also destroys
112+
// and removes all nested child views.
113+
view.unbuild(true)
114+
// build the new component. this will also create the
115+
// direct child view of the current one. it will register
116+
// itself as view.childView.
117+
var component = view.build({
118+
_meta: {
119+
$loadingRouteData: !!(dataHook && !waitForData)
123120
}
121+
})
122+
123+
// cleanup the component in case the transition is aborted
124+
// before the component is ever inserted.
125+
var cleanup = function () {
126+
component.$destroy()
127+
}
128+
129+
// actually insert the component and trigger transition
130+
var insert = function () {
124131
var router = transition.router
125132
if (router._rendered || router._transitionOnLoad) {
126133
view.transition(component)
@@ -132,18 +139,28 @@ exports.activate = function (view, transition, cb) {
132139
cb && cb()
133140
}
134141

135-
var activate = function () {
142+
// called after activation hook is resolved
143+
var afterActivate = function () {
144+
// activate the child view
145+
if (view.childView) {
146+
exports.activate(view.childView, transition)
147+
}
136148
if (dataHook && waitForData) {
137-
loadData(null, transition, dataHook, build)
149+
// wait until data loaded to insert
150+
loadData(component, transition, dataHook, insert, cleanup)
138151
} else {
139-
build()
152+
// load data and insert at the same time
153+
if (dataHook) {
154+
loadData(component, transition, dataHook)
155+
}
156+
insert()
140157
}
141158
}
142159

143160
if (activateHook) {
144-
transition.callHook(activateHook, null, activate)
161+
transition.callHook(activateHook, component, afterActivate, false, cleanup)
145162
} else {
146-
activate()
163+
afterActivate()
147164
}
148165
}
149166

@@ -169,22 +186,16 @@ exports.reuse = function (view, transition) {
169186
* @param {Transition} transition
170187
* @param {Function} hook
171188
* @param {Function} cb
189+
* @param {Function} cleanup
172190
*/
173191

174-
function loadData (component, transition, hook, cb) {
175-
if (component) {
176-
component.$loadingRouteData = true
177-
}
192+
function loadData (component, transition, hook, cb, cleanup) {
193+
component.$loadingRouteData = true
178194
transition.callHook(hook, component, function (data) {
179-
if (component) {
180-
if (data) {
181-
for (var key in data) {
182-
component.$set(key, data[key])
183-
}
184-
}
185-
component.$loadingRouteData = false
186-
} else {
187-
cb(data)
195+
for (var key in data) {
196+
component.$set(key, data[key])
188197
}
189-
})
198+
component.$loadingRouteData = false
199+
cb && cb(data)
200+
}, false, cleanup)
190201
}

src/transition.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ p.runQueue = function (queue, fn, cb) {
176176
* @param {*} [context]
177177
* @param {Function} [cb]
178178
* @param {Boolean} [expectBoolean]
179+
* @param {Function} [cleanup]
179180
*/
180181

181-
p.callHook = function (hook, context, cb, expectBoolean) {
182+
p.callHook = function (hook, context, cb, expectBoolean, cleanup) {
182183
var transition = this
183184
var nextCalled = false
184185
var next = function (data) {
@@ -194,6 +195,7 @@ p.callHook = function (hook, context, cb, expectBoolean) {
194195
}
195196
var abort = function () {
196197
transition.abort()
198+
cleanup && cleanup()
197199
}
198200
// the copied transition object passed to the user.
199201
var exposed = {

0 commit comments

Comments
 (0)