[ReactNative] Navigator pop specify route to pop

Summary:
Now you can pass a route to the pop function in the navigator context, and the Navigator will pop that scene off the stack and every scene that follows.

This changes the request API that bubbles up to the top-level navigator. The `pop` request had previously taken a route for `popToRoute`, but that is a more obscure use case. This makes the request API more closely match the context method naming.

@public

Test Plan: Verified this works by popping several routes in AdsManager. Experimented with UIExplorer Navigator example to make sure popToRoute still works as expected
This commit is contained in:
Eric Vicenti 2015-05-01 18:59:59 -07:00
parent 28e6e993c6
commit c0d77dbe9c
2 changed files with 31 additions and 7 deletions

View File

@ -387,12 +387,12 @@ var Navigator = React.createClass({
return this._handleRequest.apply(null, arguments);
},
requestPop: function() {
return this.request('pop');
requestPop: function(popToBeforeRoute) {
return this.request('pop', popToBeforeRoute);
},
requestPopTo: function(route) {
return this.request('pop', route);
return this.request('popTo', route);
},
_handleRequest: function(action, arg1, arg2) {
@ -403,6 +403,8 @@ var Navigator = React.createClass({
switch (action) {
case 'pop':
return this._handlePop(arg1);
case 'popTo':
return this._handlePopTo(arg1);
case 'push':
return this._handlePush(arg1);
default:
@ -411,11 +413,31 @@ var Navigator = React.createClass({
}
},
_handlePop: function(route) {
if (route) {
var hasRoute = this.state.routeStack.indexOf(route) !== -1;
_handlePop: function(popToBeforeRoute) {
if (popToBeforeRoute) {
var popToBeforeRouteIndex = this.state.routeStack.indexOf(popToBeforeRoute);
if (popToBeforeRouteIndex === -1) {
return false;
}
invariant(
popToBeforeRouteIndex <= this.state.presentedIndex,
'Cannot pop past a route that is forward in the navigator'
);
this._popN(this.state.presentedIndex - popToBeforeRouteIndex + 1);
return true;
}
if (this.state.presentedIndex === 0) {
return false;
}
this.pop();
return true;
},
_handlePopTo: function(destRoute) {
if (destRoute) {
var hasRoute = this.state.routeStack.indexOf(destRoute) !== -1;
if (hasRoute) {
this.popToRoute(route);
this.popToRoute(destRoute);
return true;
} else {
return false;

View File

@ -79,6 +79,8 @@ var NavigatorInterceptor = React.createClass({
switch (action) {
case 'pop':
return this.props.onPopRequest && this.props.onPopRequest(arg1, arg2);
case 'popTo':
return this.props.onPopToRequest && this.props.onPopToRequest(arg1, arg2);
case 'push':
return this.props.onPushRequest && this.props.onPushRequest(arg1, arg2);
default: