|
| 1 | +--- |
| 2 | +title: "UI-Router Sample App" |
| 3 | +excerpt: "A small, but non-trivial sample UI-Router application" |
| 4 | +layout: "single" |
| 5 | +permalink: "/sampleapp/" |
| 6 | +--- |
| 7 | +Explore the Sample App for: |
| 8 | + |
| 9 | +<div class="about_frameworks"> |
| 10 | + <div> |
| 11 | + <a href="https://ui-router.github.io/sample-app-ng1"><img src="/images/logos/angular1.png"><div>Angular 1</div></a> |
| 12 | + <a href="https://github.com/ui-router/sample-app-ng1">(src)</a> |
| 13 | + </div> |
| 14 | + <div> |
| 15 | + <a href="https://ui-router.github.io/sample-app-ng1-to-ng2"><img src="/images/logos/ng1-to-ng2.png"><div>ng1/ng2 Hybrid</div></a> |
| 16 | + <a href="https://github.com/ui-router/sample-app-ng1-to-ng2">(src)</a> |
| 17 | + </div> |
| 18 | + <div> |
| 19 | + <a><img src="/images/logos/angular2.png"><div>Angular 2</div></a> |
| 20 | + <a>(coming soon)</a> |
| 21 | + </div> |
| 22 | + <div> |
| 23 | + <a href="https://ui-router.github.io/sample-app-react/"><img src="/images/logos/react.png"><div>React</div></a> |
| 24 | + <a href="https://github.com/ui-router/sample-app-react">(src)</a> |
| 25 | + </div> |
| 26 | +</div> |
| 27 | + |
| 28 | +## UI-Router 1.0 Sample Application |
| 29 | + |
| 30 | +The sample app is intended to demonstrate a non-trivial ui-router application. |
| 31 | +It models the following |
| 32 | + |
| 33 | +- Multiple application feature modules |
| 34 | +- Authentication (simulated) |
| 35 | +- Authenticated and unauthenticated states |
| 36 | +- State lifecycle management |
| 37 | +- Application data retrieval |
| 38 | +- REST data retrieval (simulated) |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +### Visualizer |
| 43 | + |
| 44 | +We're using the [State and Transition Visualizer](http://github.com/ui-router/visualizer) to visually represent |
| 45 | +the current state tree, as well as the transitions between states. |
| 46 | +Explore how transitions work by hovering over them, and clicking to expand details (params and resolves). |
| 47 | + |
| 48 | +Note how states are _entered_ when they were previously not active, _exited_ and re-_entered_ when parameters change, |
| 49 | +and how parent states whose parameters did not change are _retained_. |
| 50 | +Each of these (_exited, entered, retained_) correspond to a Transition Hook. |
| 51 | + |
| 52 | +### Structure |
| 53 | + |
| 54 | +The application utilizes ES6 modules. |
| 55 | + |
| 56 | +There are many ways to structure a ui-router app. |
| 57 | +We aren't super opinionated on application structure. |
| 58 | +Use what works for you. |
| 59 | +We organized ours in the following way: |
| 60 | + |
| 61 | +- Sub-module (feature) organization |
| 62 | + - Each feature gets its own directory. |
| 63 | + - Features contain state definitions, components, and other code such as services. |
| 64 | +- Leverage ES6 modules |
| 65 | + - Each thing (component, state, service) is defined in its own file (ES6 module) and is exported. |
| 66 | + - Each feature module has an index file which imports the code, and composes them together (as the feature module), then re-exports them. |
| 67 | + - At the top level, the bootstrap imports all the features and composes the application, registering serviecs and states with UI-Router, etc. |
| 68 | + |
| 69 | +### UI-Router Patterns |
| 70 | + |
| 71 | +- Default substate for a top-level state |
| 72 | + - Example: the `mymessages` state specifies `redirectTo: 'mymessages.folder'`. |
| 73 | + When the user tries to activate `mymessages` (e.g., using a ui-sref link or a bookmark), the transition is redirected to `mymessages.folder`. |
| 74 | + |
| 75 | +- Defining a default parameter for a state |
| 76 | + - Example: the `folderId` parameter of the `folder` state defaults to a value of 'inbox'. |
| 77 | + |
| 78 | +- Application data lifecycle (resolve) |
| 79 | + - Data loading is managed by the state declaration, via the `resolve:` block |
| 80 | + - Data is fetched before the state is _entered_ |
| 81 | + - Data is fetched according to state parameters |
| 82 | + - The state is _entered_ when the data is ready |
| 83 | + - The resolved data is provided to the components |
| 84 | + - The resolve data is accessible until the state is exited |
| 85 | + |
| 86 | +- Data dirty checking and confirmation hooks |
| 87 | + - Example: The `contacts.edit` state allows a user to edit contact details using a form. |
| 88 | + If a user has modified data in the form, then tries to activate a different state, |
| 89 | + the edit contact component will prompt for confirmation before allowing the transition to exit `contacts.edit`. |
| 90 | + |
| 91 | +- Custom application behaviors using declarative Transition Hooks. |
| 92 | + - The `authRequired` hook checks for state metadata, which declares that a state requires authentication. |
| 93 | + When a transition starts, the inspects the destination state's `data.authRequired: true` property. |
| 94 | + If it's truthy, then it checks that the user is authenticated, or redirects to the login state. |
| 95 | + |
0 commit comments