Skip to content

Commit a50285c

Browse files
committed
fix multiple data hooks handling
1 parent b2ed17a commit a50285c

File tree

3 files changed

+15
-39
lines changed

3 files changed

+15
-39
lines changed

src/pipeline.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,14 @@ export function reuse (view, transition) {
260260

261261
function loadData (component, transition, hook, cb, cleanup) {
262262
component.$loadingRouteData = true
263-
transition.callHooks(hook, component, cb, {
263+
transition.callHooks(hook, component, () => {
264+
component.$loadingRouteData = false
265+
component.$emit('route-data-loaded', component)
266+
cb && cb()
267+
}, {
264268
cleanup,
265269
postActivate: true,
266270
processData: (data) => {
267-
// merge data from multiple data hooks
268-
if (Array.isArray(data) && data._needMerge) {
269-
data = data.reduce((res, obj) => {
270-
if (isPlainObject(obj)) {
271-
Object.keys(obj).forEach(key => {
272-
res[key] = obj[key]
273-
})
274-
}
275-
return res
276-
}, Object.create(null))
277-
}
278271
// handle promise sugar syntax
279272
const promises = []
280273
if (isPlainObject(data)) {
@@ -289,14 +282,8 @@ function loadData (component, transition, hook, cb, cleanup) {
289282
}
290283
})
291284
}
292-
const onDataLoaded = () => {
293-
component.$loadingRouteData = false
294-
component.$emit('route-data-loaded', component)
295-
}
296-
if (!promises.length) {
297-
onDataLoaded()
298-
} else {
299-
return promises[0].constructor.all(promises).then(onDataLoaded)
285+
if (promises.length) {
286+
return promises[0].constructor.all(promises)
300287
}
301288
}
302289
})

src/transition.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,7 @@ export default class RouteTransition {
213213

214214
// handle errors
215215
const onError = err => {
216-
if (postActivate) {
217-
if (!nextCalled) next()
218-
} else {
219-
abort()
220-
}
216+
postActivate ? next() : abort()
221217
if (err && !transition.router._suppress) {
222218
warn('Uncaught error during transition: ')
223219
throw err instanceof Error ? err : new Error(err)
@@ -324,18 +320,11 @@ export default class RouteTransition {
324320

325321
callHooks (hooks, context, cb, options) {
326322
if (Array.isArray(hooks)) {
327-
const res = []
328-
res._needMerge = true
329323
this.runQueue(hooks, (hook, _, next) => {
330324
if (!this.aborted) {
331-
this.callHook(hook, context, r => {
332-
if (r) res.push(r)
333-
next()
334-
}, options)
325+
this.callHook(hook, context, next, options)
335326
}
336-
}, () => {
337-
cb(res)
338-
})
327+
}, cb)
339328
} else {
340329
this.callHook(hooks, context, cb, options)
341330
}

test/unit/specs/pipeline/data.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ describe('data', function () {
141141
assertCalls(calls, ['data.data'])
142142
expect(router.app.$el.textContent).toBe('loading...')
143143
setTimeout(function () {
144-
// should not abort, but nor should finish loading data
145-
expect(router.app.$el.textContent).toBe('loading...')
144+
// should not abort
145+
expect(router.app.$el.textContent).toBe('default')
146146
done()
147147
}, wait * 2)
148148
})
@@ -190,8 +190,8 @@ describe('data', function () {
190190
assertCalls(calls, ['data.data'])
191191
expect(router.app.$el.textContent).toBe('loading...')
192192
setTimeout(function () {
193-
// should not abort, but nor should finish loading data
194-
expect(router.app.$el.textContent).toBe('loading...')
193+
// should not abort
194+
expect(router.app.$el.textContent).toBe('default')
195195
done()
196196
}, wait * 2)
197197
})
@@ -238,7 +238,7 @@ describe('data', function () {
238238
setTimeout(function () {
239239
expect(router.app.$el.textContent).toBe('hello hello2 hello3')
240240
done()
241-
}, wait * 3)
241+
}, wait * 4)
242242
})
243243
})
244244
})

0 commit comments

Comments
 (0)