diff --git a/src/State.js b/src/State.js index c2e1856b5..fcfe0d84b 100644 --- a/src/State.js +++ b/src/State.js @@ -8,10 +8,34 @@ */ import assert from "assert"; +function getStateFromScenes(route, scenes, props) { + const getters = []; + let result = {}; + let scene = route; + while (scene) { + if (scene.getInitialState) { + getters.push(scene.getInitialState); + } + scene = scenes[scene.parent]; + } + + getters.reverse().forEach(fn => { + result = {...result, ...fn(props)}; + }); + + return result; +} + export function getInitialState(route:{string: any},scenes:{string:any}, position=0, props={}){ const {key, style, type, ...parentProps} = props; if (!route.children){ - return { ...scenes.rootProps, ...route, key:position+"_"+route.sceneKey, ...parentProps,}; + return { + ...scenes.rootProps, + ...route, + key:position+"_"+route.sceneKey, + ...parentProps, + ...getStateFromScenes(route, scenes, props), + }; } let {children, ...res} = {...route, ...parentProps}; let index = 0; diff --git a/test/Actions.test.js b/test/Actions.test.js index 2768b14f4..6644fe286 100644 --- a/test/Actions.test.js +++ b/test/Actions.test.js @@ -5,30 +5,32 @@ import React from 'react-native'; import Scene from '../src/Scene'; import createReducer from '../src/Reducer'; -const scenesData = - - - - - - - - - - - - - - - - - - - -; - describe('Actions', () => { it('should create needed actions', () => { + let id = 0; + const guid = () => id++; + + const scenesData = ({ foo: guid() })}> + + + + + + + + + + ({ foo: 'what', bar: guid() })}/> + + + + + + + + + + ; const scenes = Actions.create(scenesData); expect(scenes.conversations.component).to.equal("Conversations"); @@ -57,6 +59,8 @@ describe('Actions', () => { expect(state).equal(undefined); Actions.init(); expect(state.key).equal("0_modal"); + expect(state.children[0].children[0].children[0].children[0].bar).equal(1); + expect(state.children[0].children[0].children[0].children[0].foo).equal('what'); Actions.messaging(); expect(currentScene.key).equal("messaging"); @@ -69,6 +73,9 @@ describe('Actions', () => { Actions.pop(); expect(state.from.key).equal("1_login"); + Actions.launch(); + expect(state.children[1].foo).equal(3); + expect(state.children[1].bar).equal(undefined); }); });