Skip to content

Commit 2ac2510

Browse files
committed
[added] router.replaceRoutes(children)
1 parent 12be8e1 commit 2ac2510

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

modules/utils/createRouter.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ function createRouter(options) {
157157
var state = {};
158158
var nextState = {};
159159
var pendingTransition = null;
160+
var dispatchHandler = null;
160161
var changeListener = null;
161162

162163
function cancelPendingTransition() {
@@ -209,6 +210,19 @@ function createRouter(options) {
209210
routes.push.apply(routes, createRoutesFromChildren(children, this, namedRoutes));
210211
},
211212

213+
/**
214+
* Replaces routes of this router from the given children object (see ReactChildren).
215+
*/
216+
replaceRoutes: function (children) {
217+
cancelPendingTransition();
218+
219+
routes = [];
220+
namedRoutes = {};
221+
222+
this.addRoutes(children);
223+
this.refresh();
224+
},
225+
212226
/**
213227
* Returns an absolute URL path created from the given route
214228
* name, URL parameters, and query.
@@ -325,11 +339,13 @@ function createRouter(options) {
325339
* transition. To resolve asynchronously, they may use the callback argument. If no
326340
* hooks wait, the transition is fully synchronous.
327341
*/
328-
dispatch: function (path, action, callback) {
342+
dispatch: function (path, action) {
329343
cancelPendingTransition();
330344

331345
var prevPath = state.path;
332-
if (prevPath === path)
346+
var isRefreshing = action == null;
347+
348+
if (prevPath === path && !isRefreshing)
333349
return; // Nothing to do!
334350

335351
// Record the scroll position as early as possible to
@@ -378,11 +394,11 @@ function createRouter(options) {
378394

379395
transition.from(fromRoutes, fromComponents, function (error) {
380396
if (error || transition.isAborted)
381-
return callback.call(router, error, transition);
397+
return dispatchHandler.call(router, error, transition);
382398

383399
transition.to(toRoutes, nextParams, nextQuery, function (error) {
384400
if (error || transition.isAborted)
385-
return callback.call(router, error, transition);
401+
return dispatchHandler.call(router, error, transition);
386402

387403
nextState.path = path;
388404
nextState.action = action;
@@ -391,7 +407,7 @@ function createRouter(options) {
391407
nextState.params = nextParams;
392408
nextState.query = nextQuery;
393409

394-
callback.call(router, null, transition);
410+
dispatchHandler.call(router, null, transition);
395411
});
396412
});
397413
},
@@ -409,7 +425,7 @@ function createRouter(options) {
409425
'Router is already running'
410426
);
411427

412-
var dispatchHandler = function (error, transition) {
428+
dispatchHandler = function (error, transition) {
413429
if (error)
414430
onError.call(router, error);
415431

@@ -426,18 +442,18 @@ function createRouter(options) {
426442
};
427443

428444
if (typeof location === 'string') {
429-
router.dispatch(location, null, dispatchHandler);
445+
router.dispatch(location, null);
430446
} else {
431447
// Listen for changes to the location.
432448
changeListener = function (change) {
433-
router.dispatch(change.path, change.type, dispatchHandler);
449+
router.dispatch(change.path, change.type);
434450
};
435451

436452
if (location.addChangeListener)
437453
location.addChangeListener(changeListener);
438454

439455
// Bootstrap using the current path.
440-
router.dispatch(location.getCurrentPath(), null, dispatchHandler);
456+
this.refresh();
441457

442458
this.isRunning = true;
443459
}
@@ -452,6 +468,10 @@ function createRouter(options) {
452468
}
453469

454470
this.isRunning = false;
471+
},
472+
473+
refresh: function () {
474+
router.dispatch(location.getCurrentPath(), null);
455475
}
456476

457477
},

0 commit comments

Comments
 (0)