@@ -31,30 +31,6 @@ var DEFAULT_LOCATION = canUseDOM ? HashLocation : '/';
31
31
*/
32
32
var DEFAULT_SCROLL_BEHAVIOR = canUseDOM ? ImitateBrowserBehavior : null ;
33
33
34
- /**
35
- * The default error handler for new routers.
36
- */
37
- function defaultErrorHandler ( error ) {
38
- // Throw so we don't silently swallow async errors.
39
- throw error ; // This error probably originated in a transition hook.
40
- }
41
-
42
- /**
43
- * The default aborted transition handler for new routers.
44
- */
45
- function defaultAbortHandler ( abortReason , location ) {
46
- if ( typeof location === 'string' )
47
- throw new Error ( 'Unhandled aborted transition! Reason: ' + abortReason ) ;
48
-
49
- if ( abortReason instanceof Cancellation ) {
50
- return ;
51
- } else if ( abortReason instanceof Redirect ) {
52
- location . replace ( this . makePath ( abortReason . to , abortReason . params , abortReason . query ) ) ;
53
- } else {
54
- location . pop ( ) ;
55
- }
56
- }
57
-
58
34
function createMatch ( route , params , pathname , query ) {
59
35
return {
60
36
routes : [ route ] ,
@@ -152,13 +128,10 @@ function createRouter(options) {
152
128
var mountedComponents = [ ] ;
153
129
var location = options . location || DEFAULT_LOCATION ;
154
130
var scrollBehavior = options . scrollBehavior || DEFAULT_SCROLL_BEHAVIOR ;
155
- var onError = options . onError || defaultErrorHandler ;
156
- var onAbort = options . onAbort || defaultAbortHandler ;
157
131
var state = { } ;
158
132
var nextState = { } ;
159
133
var pendingTransition = null ;
160
134
var dispatchHandler = null ;
161
- var changeListener = null ;
162
135
163
136
if ( typeof location === 'string' ) {
164
137
warning (
@@ -322,6 +295,28 @@ function createRouter(options) {
322
295
return false ;
323
296
} ,
324
297
298
+ handleAbort : options . onAbort || function ( abortReason ) {
299
+ if ( typeof location === 'string' )
300
+ throw new Error ( 'Unhandled aborted transition! Reason: ' + abortReason ) ;
301
+
302
+ if ( abortReason instanceof Cancellation ) {
303
+ return ;
304
+ } else if ( abortReason instanceof Redirect ) {
305
+ location . replace ( this . makePath ( abortReason . to , abortReason . params , abortReason . query ) ) ;
306
+ } else {
307
+ location . pop ( ) ;
308
+ }
309
+ } ,
310
+
311
+ handleError : options . onError || function ( error ) {
312
+ // Throw so we don't silently swallow async errors.
313
+ throw error ; // This error probably originated in a transition hook.
314
+ } ,
315
+
316
+ handleLocationChange : function ( change ) {
317
+ this . dispatch ( change . path , change . type ) ;
318
+ } ,
319
+
325
320
/**
326
321
* Performs a transition to the given path and calls callback(error, abortReason)
327
322
* when the transition is finished. If both arguments are null the router's state
@@ -392,22 +387,17 @@ function createRouter(options) {
392
387
393
388
transition . from ( fromRoutes , fromComponents , function ( error ) {
394
389
if ( error || transition . abortReason )
395
- return dispatchHandler . call ( Router , error , transition ) ;
390
+ return dispatchHandler . call ( Router , error , transition ) ; // No need to continue.
396
391
397
392
transition . to ( toRoutes , nextParams , nextQuery , function ( error ) {
398
- if ( error || transition . abortReason )
399
- return dispatchHandler . call ( Router , error , transition ) ;
400
-
401
- nextState = {
393
+ dispatchHandler . call ( Router , error , transition , {
402
394
path : path ,
403
395
action : action ,
404
396
pathname : match . pathname ,
405
397
routes : nextRoutes ,
406
398
params : nextParams ,
407
399
query : nextQuery
408
- } ;
409
-
410
- dispatchHandler . call ( Router , null , transition ) ;
400
+ } ) ;
411
401
} ) ;
412
402
} ) ;
413
403
} ,
@@ -425,53 +415,46 @@ function createRouter(options) {
425
415
'Router is already running'
426
416
) ;
427
417
428
- dispatchHandler = function ( error , transition ) {
418
+ dispatchHandler = function ( error , transition , newState ) {
429
419
if ( error )
430
- onError . call ( Router , error ) ;
420
+ Router . handleError ( error ) ;
431
421
432
422
if ( pendingTransition !== transition )
433
423
return ;
434
424
435
425
pendingTransition = null ;
436
426
437
427
if ( transition . abortReason ) {
438
- onAbort . call ( Router , transition . abortReason , location ) ;
428
+ Router . handleAbort ( transition . abortReason ) ;
439
429
} else {
440
- callback . call ( Router , Router , nextState ) ;
430
+ callback . call ( this , this , nextState = newState ) ;
441
431
}
442
432
} ;
443
433
444
434
if ( typeof location === 'string' ) {
445
435
Router . dispatch ( location , null ) ;
446
436
} else {
447
- // Listen for changes to the location.
448
- changeListener = function ( change ) {
449
- Router . dispatch ( change . path , change . type ) ;
450
- } ;
451
-
452
437
if ( location . addChangeListener )
453
- location . addChangeListener ( changeListener ) ;
438
+ location . addChangeListener ( Router . handleLocationChange ) ;
439
+
440
+ this . isRunning = true ;
454
441
455
442
// Bootstrap using the current path.
456
443
this . refresh ( ) ;
457
-
458
- this . isRunning = true ;
459
444
}
460
445
} ,
461
446
447
+ refresh : function ( ) {
448
+ Router . dispatch ( location . getCurrentPath ( ) , null ) ;
449
+ } ,
450
+
462
451
stop : function ( ) {
463
452
this . cancelPendingTransition ( ) ;
464
453
465
- if ( location . removeChangeListener && changeListener ) {
466
- location . removeChangeListener ( changeListener ) ;
467
- changeListener = null ;
468
- }
454
+ if ( location . removeChangeListener )
455
+ location . removeChangeListener ( Router . handleLocationChange ) ;
469
456
470
457
this . isRunning = false ;
471
- } ,
472
-
473
- refresh : function ( ) {
474
- Router . dispatch ( location . getCurrentPath ( ) , null ) ;
475
458
}
476
459
477
460
} ,
0 commit comments