Skip to content

Commit 8343f8e

Browse files
author
aksonov
committed
add POP_AND_PUSH action to emulate better 'replace' handling
1 parent 54652fa commit 8343f8e

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

Example/Example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const Example = () => {
6565
<Scene key="register" back>
6666
<Scene key="_register" component={Register} title="Register"/>
6767
<Scene key="register2" component={Register} title="Register2"/>
68-
<Scene key="home" component={Home} title="Replace" type={ActionConst.REPLACE}/>
68+
<Scene key="home" component={Home} title="Replace" type={ActionConst.POP_AND_PUSH}/>
6969
</Scene>
7070
<Scene key="launch" component={Launch} title="Launch" initial/>
7171
<Scene

dist/ActionConst.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ var REFRESH=exports.REFRESH='REACT_NATIVE_ROUTER_FLUX_REFRESH';
1010
var RESET=exports.RESET='REACT_NATIVE_ROUTER_FLUX_RESET';
1111
var FOCUS=exports.FOCUS='REACT_NATIVE_ROUTER_FLUX_FOCUS';
1212
var BLUR=exports.BLUR='REACT_NATIVE_ROUTER_FLUX_BLUR';
13-
var ANDROID_BACK=exports.ANDROID_BACK='REACT_NATIVE_ROUTER_FLUX_ANDROID_BACK';
13+
var ANDROID_BACK=exports.ANDROID_BACK='REACT_NATIVE_ROUTER_FLUX_ANDROID_BACK';
14+
var POP_AND_PUSH=exports.POP_AND_PUSH='REACT_NATIVE_ROUTER_FLUX_POP_AND_PUSH';

dist/navigationStore.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ ActionConst.POP_AND_REPLACE,'pop'),_defineProperty(_actionMap,
1313
ActionConst.POP_TO,'popTo'),_defineProperty(_actionMap,
1414
ActionConst.REFRESH,'refresh'),_defineProperty(_actionMap,
1515
ActionConst.RESET,'reset'),_defineProperty(_actionMap,
16-
ActionConst.PUSH_OR_POP,'push'),_actionMap);
16+
ActionConst.PUSH_OR_POP,'push'),_defineProperty(_actionMap,
17+
ActionConst.POP_AND_PUSH,'popAndPush'),_actionMap);
1718

1819

1920
var supportedActions=exports.supportedActions=(_supportedActions={},_defineProperty(_supportedActions,
2021
ActionConst.PUSH,_reactNavigation.NavigationActions.NAVIGATE),_defineProperty(_supportedActions,
2122
ActionConst.JUMP,_reactNavigation.NavigationActions.NAVIGATE),_defineProperty(_supportedActions,
2223
ActionConst.BACK,_reactNavigation.NavigationActions.BACK),_defineProperty(_supportedActions,
2324
ActionConst.REFRESH,_reactNavigation.NavigationActions.BACK),_defineProperty(_supportedActions,
24-
ActionConst.RESET,_reactNavigation.NavigationActions.RESET),_supportedActions);
25+
ActionConst.RESET,_reactNavigation.NavigationActions.RESET),_defineProperty(_supportedActions,
26+
ActionConst.REPLACE,_reactNavigation.NavigationActions.RESET),_supportedActions);
2527

2628
function filterParam(data){
2729
if(data.toString()!=='[object Object]'){
@@ -193,7 +195,7 @@ currentState=newState;
193195
if(nextScene===routeName){
194196
_this.setState(newState);
195197
}
196-
}else if(type===ActionConst.REPLACE){
198+
}else if(type===ActionConst.POP_AND_PUSH){
197199
_this.pop();
198200
_this.push.apply(_this,[routeName].concat(params));
199201
}
@@ -245,14 +247,22 @@ _this.refresh(res.refresh);
245247
};this.
246248

247249
popTo=function(routeName){for(var _len5=arguments.length,params=Array(_len5>1?_len5-1:0),_key5=1;_key5<_len5;_key5++){params[_key5-1]=arguments[_key5];}
248-
_this.run.apply(_this,[ActionConst.POP_TO,routeName].concat(params));
250+
_this.run.apply(_this,[ActionConst.POP_TO,routeName,null].concat(params));
249251
};this.
250252

251-
replace=function(routeName){for(var _len6=arguments.length,params=Array(_len6>1?_len6-1:0),_key6=1;_key6<_len6;_key6++){params[_key6-1]=arguments[_key6];}
252-
_this.run.apply(_this,[ActionConst.REPLACE,routeName].concat(params));
253+
popAndPush=function(routeName){for(var _len6=arguments.length,params=Array(_len6>1?_len6-1:0),_key6=1;_key6<_len6;_key6++){params[_key6-1]=arguments[_key6];}
254+
_this.run.apply(_this,[ActionConst.POP_AND_PUSH,routeName,null].concat(params));
253255
};this.
254256

255-
reset=function(routeName){for(var _len7=arguments.length,params=Array(_len7>1?_len7-1:0),_key7=1;_key7<_len7;_key7++){params[_key7-1]=arguments[_key7];}
257+
replace=function(routeName){for(var _len7=arguments.length,params=Array(_len7>1?_len7-1:0),_key7=1;_key7<_len7;_key7++){params[_key7-1]=arguments[_key7];}
258+
var res=uniteParams(routeName,params);
259+
_this.run(ActionConst.REPLACE,routeName,{key:routeName,index:0,actions:[_reactNavigation.NavigationActions.navigate({
260+
routeName:routeName,
261+
params:res})]});
262+
263+
};this.
264+
265+
reset=function(routeName){for(var _len8=arguments.length,params=Array(_len8>1?_len8-1:0),_key8=1;_key8<_len8;_key8++){params[_key8-1]=arguments[_key8];}
256266
var res=uniteParams(routeName,params);
257267
_this.run(ActionConst.RESET,routeName,{key:null,index:0,actions:[_reactNavigation.NavigationActions.navigate({
258268
routeName:routeName,

src/ActionConst.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export const RESET = 'REACT_NATIVE_ROUTER_FLUX_RESET';
1111
export const FOCUS = 'REACT_NATIVE_ROUTER_FLUX_FOCUS';
1212
export const BLUR = 'REACT_NATIVE_ROUTER_FLUX_BLUR';
1313
export const ANDROID_BACK = 'REACT_NATIVE_ROUTER_FLUX_ANDROID_BACK';
14+
export const POP_AND_PUSH = 'REACT_NATIVE_ROUTER_FLUX_POP_AND_PUSH';

src/navigationStore.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const actionMap = {
1414
[ActionConst.REFRESH]: 'refresh',
1515
[ActionConst.RESET]: 'reset',
1616
[ActionConst.PUSH_OR_POP]: 'push',
17+
[ActionConst.POP_AND_PUSH]: 'popAndPush',
1718
};
1819

1920
export const supportedActions = {
@@ -22,6 +23,7 @@ export const supportedActions = {
2223
[ActionConst.BACK]: NavigationActions.BACK,
2324
[ActionConst.REFRESH]: NavigationActions.BACK,
2425
[ActionConst.RESET]: NavigationActions.RESET,
26+
[ActionConst.REPLACE]: NavigationActions.RESET,
2527
};
2628
function filterParam(data) {
2729
if (data.toString() !== '[object Object]') {
@@ -193,7 +195,7 @@ class NavigationStore {
193195
if (nextScene === routeName) {
194196
this.setState(newState);
195197
}
196-
} else if (type === ActionConst.REPLACE) {
198+
} else if (type === ActionConst.POP_AND_PUSH) {
197199
this.pop();
198200
this.push(routeName, ...params);
199201
}
@@ -245,11 +247,19 @@ class NavigationStore {
245247
};
246248

247249
popTo = (routeName, ...params) => {
248-
this.run(ActionConst.POP_TO, routeName, ...params);
250+
this.run(ActionConst.POP_TO, routeName, null, ...params);
251+
};
252+
253+
popAndPush = (routeName, ...params) => {
254+
this.run(ActionConst.POP_AND_PUSH, routeName, null, ...params);
249255
};
250256

251257
replace = (routeName, ...params) => {
252-
this.run(ActionConst.REPLACE, routeName, ...params);
258+
const res = uniteParams(routeName, params);
259+
this.run(ActionConst.REPLACE, routeName, { key: routeName, index: 0, actions: [NavigationActions.navigate({
260+
routeName,
261+
params: res,
262+
})] });
253263
};
254264

255265
reset = (routeName, ...params) => {

0 commit comments

Comments
 (0)