|
| 1 | +# Web Router |
| 2 | + |
| 3 | +An extensive routing solution for web. |
| 4 | + |
| 5 | +## History |
| 6 | + |
| 7 | +#### browserHistory : |
| 8 | + |
| 9 | +is for use in modern web browsers that support the [HTML5 history API](http://diveintohtml5.info/history.html) (see [cross-browser compatibility](http://caniuse.com/#feat=history)) |
| 10 | + |
| 11 | +***Options*** : |
| 12 | + |
| 13 | +`basename` (default = "") : The base URL of the app |
| 14 | + |
| 15 | +`forceRefresh` (default = false) : Set true to force full page refreshes |
| 16 | + |
| 17 | +`keyLength` (default = 6) : The length of location.key |
| 18 | + |
| 19 | + |
| 20 | +Example : |
| 21 | + |
| 22 | +```scala |
| 23 | +val history = HistoryFactory.browserHistory(options = BrowserHistoryOptions(..)) |
| 24 | +``` |
| 25 | + |
| 26 | + |
| 27 | +#### hashHistory : |
| 28 | + |
| 29 | +is for use in legacy web browsers |
| 30 | + |
| 31 | +***Options*** : |
| 32 | + |
| 33 | +`basename` (default = "") : The base URL of the app |
| 34 | + |
| 35 | +`hashType` (default = "slash") : The hash type to use |
| 36 | + |
| 37 | +Example : |
| 38 | + |
| 39 | +```scala |
| 40 | +val history = HistoryFactory.memoryHistory(options = HashHistoryOptions(..)) |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +## Routes |
| 45 | + |
| 46 | +A route can be static(constant for entire lifecycle of the app) or dynamic(varies over the lifecycle of the app),route can optionally store a state(works only in browserHistory).In react we usually define ScreenComponent for each route in our app. |
| 47 | + |
| 48 | +### Static Routes/URLS : |
| 49 | + |
| 50 | +Examples : `about` , `sponsors`,.. |
| 51 | + |
| 52 | + |
| 53 | +1) Route Component with No LocationState and ComponentState |
| 54 | + |
| 55 | +```scala |
| 56 | +@ScalaJSDefined |
| 57 | +class AboutScreen extends RouterScreenComponentNoPSLS { |
| 58 | + |
| 59 | + def render() = { |
| 60 | + ... |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +//while registering |
| 65 | + |
| 66 | +registerScreen[AboutScreen]("about") |
| 67 | + |
| 68 | +// while navigating |
| 69 | +navigation.navigate[AboutScreen]() |
| 70 | +``` |
| 71 | + |
| 72 | +2) Route Component with LocationState and No ComponentState |
| 73 | + |
| 74 | + |
| 75 | +```scala |
| 76 | +@ScalaJSDefined |
| 77 | +class AboutScreen extends RouterScreenComponentLS[AboutScreen.LocationState] { |
| 78 | + |
| 79 | + def render() = { |
| 80 | + val id = locationState.id |
| 81 | + ... |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +object AboutScreen { |
| 86 | + case class LocationState(id:String) |
| 87 | +} |
| 88 | + |
| 89 | +//while registering |
| 90 | + |
| 91 | +registerScreen[AboutScreen]("about") |
| 92 | + |
| 93 | +// while navigating |
| 94 | +navigation.navigateLS[AboutScreen](state = AboutScreen.LocationState("123")) |
| 95 | +``` |
| 96 | + |
| 97 | +***Note: It only works for browserHistory*** |
| 98 | + |
| 99 | +3) Route Component with ComponentState and No LocationState |
| 100 | + |
| 101 | + |
| 102 | +```scala |
| 103 | +@ScalaJSDefined |
| 104 | +class AboutScreen extends RouterScreenComponentS[AboutScreen.State] { |
| 105 | + |
| 106 | + initialState(State(true)) |
| 107 | + def render() = { |
| 108 | + if(state.loading) "spinner" |
| 109 | + else ... |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +object AboutScreen { |
| 114 | + case class State(loading:Boolean) |
| 115 | +} |
| 116 | + |
| 117 | +//while registering |
| 118 | + |
| 119 | +registerScreen[AboutScreen]("about") |
| 120 | + |
| 121 | +// while navigating |
| 122 | +navigation.navigate[AboutScreen]() |
| 123 | +``` |
| 124 | + |
| 125 | + |
| 126 | +### Dynamic Routes/URLS : |
| 127 | + |
| 128 | +Examples : `users/:id/info`, `problems/:id` |
| 129 | + |
| 130 | +1) Route Component with No LocationState and ComponentState |
| 131 | + |
| 132 | +```scala |
| 133 | +@ScalaJSDefined |
| 134 | +class UserInfoScreen extends RouterScreenComponentP[UserInfoScreen.Params] { |
| 135 | + |
| 136 | + def render() = { |
| 137 | + val user = UsersService.getUserById(params.id) |
| 138 | + ... |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +object UserInfoScreen { |
| 143 | + |
| 144 | + @ScalaJSDefined |
| 145 | + trait Params extends js.Object { |
| 146 | + val id:String |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +//while registering |
| 151 | + |
| 152 | +registerDynamicScreen[UserInfoScreen]("users/:id/info") |
| 153 | + |
| 154 | +// while navigating |
| 155 | +navigation.navigateP[UserInfoScreen](params = new UserInfoScreen.Params {overrid val id = "userid1234"}) |
| 156 | +``` |
| 157 | + |
| 158 | +2) Route Component with LocationState and No ComponentState |
| 159 | + |
| 160 | + |
| 161 | +```scala |
| 162 | +@ScalaJSDefined |
| 163 | +class UserInfoScreen extends RouterScreenComponentPLS[UserInfoScreen.Params,UserInfoScreen.LocationState] { |
| 164 | + |
| 165 | + def render() = { |
| 166 | + val user = UsersService.getUserById(params.id) |
| 167 | + val |
| 168 | + ... |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +object UserInfoScreen { |
| 173 | + |
| 174 | + case class LocationState(sid:String) |
| 175 | + |
| 176 | + @ScalaJSDefined |
| 177 | + trait Params extends js.Object { |
| 178 | + val id:String |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +//while registering |
| 183 | + |
| 184 | +registerDynamicScreen[UserInfoScreen]("users/:id/info") |
| 185 | + |
| 186 | +// while navigating |
| 187 | +navigation.navigatePLS[UserInfoScreen](params = new UserInfoScreen.Params {overrid val id = "userid1234"}, |
| 188 | +state = UserInfoScreen.LocationState("sid")) |
| 189 | +``` |
| 190 | + |
| 191 | +***Note: It only works for browserHistory*** |
| 192 | + |
| 193 | +3) Route Component with ComponentState and No LocationState |
| 194 | + |
| 195 | +```scala |
| 196 | +@ScalaJSDefined |
| 197 | +class UserInfoScreen extends RouterScreenComponentPS[UserInfoScreen.Params,UserInfoScreen.State] { |
| 198 | + initialState(State(true)) |
| 199 | + def render() = { |
| 200 | + val user = UsersService.getUserById(params.id) |
| 201 | + if(state.loading) .. |
| 202 | + else ... |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +object UserInfoScreen { |
| 207 | + |
| 208 | + case class State(loading:Boolean) |
| 209 | + @ScalaJSDefined |
| 210 | + trait Params extends js.Object { |
| 211 | + val id:String |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +//while registering |
| 216 | + |
| 217 | +registerDynamicScreen[UserInfoScreen]("users/:id/info") |
| 218 | + |
| 219 | +// while navigating |
| 220 | +navigation.navigateP[UserInfoScreen](params = new UserInfoScreen.Params {overrid val id = "userid1234"}) |
| 221 | +``` |
| 222 | + |
| 223 | +## Example |
| 224 | + |
| 225 | +```scala |
| 226 | + |
| 227 | +object AppRoutes extends RouterConfig { |
| 228 | + |
| 229 | + override val history: History = HistoryFactory.browserHistory() |
| 230 | + |
| 231 | + override val initialRoute = registerInitialScreen[HomeScreen] |
| 232 | + registerScreen[AboutScreen]("about") |
| 233 | + registerDynamicScreen[ThirdScreenWithParams]("users/:id/info") |
| 234 | + |
| 235 | + registerModule(ProblemsModuleRoutes) |
| 236 | + |
| 237 | + override val notFound: RouteNotFound = RouteNotFound(initialRoute._1) |
| 238 | +} |
| 239 | + |
| 240 | + |
| 241 | +object ProblemsModuleRoutes extends RouterModuleConfig("problems") { |
| 242 | + |
| 243 | + registerScreen[ProblemsListScreen]("/") |
| 244 | + registerDynamicScreen[ProblemInfoScreen](":id/info") |
| 245 | +} |
| 246 | + |
| 247 | +val root = Router(AppRoutes) |
| 248 | + |
| 249 | +ReactDOM.render(root,dom.document.getElementByID("app")) |
| 250 | + |
| 251 | +``` |
0 commit comments