Skip to content

Commit 71d0316

Browse files
with a router
1 parent 4fc68b0 commit 71d0316

File tree

1 file changed

+91
-7
lines changed

1 file changed

+91
-7
lines changed

Diff for: section_1/readme.md

+91-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This section will cover the following topics:
44

55
+ [Creating a new Rail app with Hyperstack](#creating-a-new-rail-app-with-hyperstack)
6-
+ Adding a SPA Router
6+
+ [Adding a SPA Router](#adding-a-spa-router)
77
+ Using JavaScript libraries
88
+ Working with Isomorphic models and Policies
99
+ Deploying to production
@@ -14,33 +14,33 @@ Creating a new Rails application with Hyperstack all installed is very simple:
1414

1515
Create a new rails application:
1616

17-
```
17+
```bash
1818
rails new MyApp
1919
```
2020

2121
Add the Hyperstack gem:
2222

23-
```
23+
```bash
2424
cd MyApp
2525

2626
bundle add 'rails-hyperstack' --version "~> 1.0.alpha1.0"
2727
```
2828

2929
Run the installer (which will create all the Hyperstack folders and config)
3030

31-
```
31+
```bash
3232
bundle exec rails hyperstack:install
3333
```
3434

3535
And that's it, Hyperstack is installed.
3636

3737
Run the server and navigate load the webpage in your browser:
3838

39-
```
39+
```bash
4040
foreman start
4141
```
4242

43-
Navigate to:
43+
Navigate to:
4444

4545
`http://localhost:5000/`
4646

@@ -50,8 +50,92 @@ You should see a page with the word `App` - hooray you are installed!
5050

5151
+ `app\hyperstack` is where all the Hyperstack code lives. This folder basically replaces your Rails MVC folders. The `component` sub-folder contains your Hyperstack components and the `models` sub-folder your Isomorphic models (you can still keep server-only models in your normal models folder).
5252
+ `app\javascript` is a Webpacker folder containing js files known as packs. These are mich like gem files for the front end, they are the JS packages Webpack will compile and deliver to the front-end. Wer will cover these in more detail later in this tutorial.
53-
+ `app\policies` contain your policy files which grant or deny access to models depending on who the current user is. We will cover policies later in this tutorial. For now, just note that a default (development only) completely open policy has been added.
53+
+ `app\policies` contain your policy files which grant or deny access to models depending on who the current user is. We will cover policies later in this tutorial. For now, just note that a default (development only) completely open policy has been added.
5454
+ `initializers\hyperstack.rb` contains all the Hyperstack optional initialization.
5555
+ `Procfile` is a configuration file used by the Foreman gem to start the rails server and also run a Hotloader process. When you type `foreman start` this file is used.
5656
+ `package.json` is used by Webpacker and Yarn (basically like a gemfile) to specify which JS libraries will be required.
5757

58+
## Adding a SPA Router
59+
60+
Unlike traditional multi-page-applications, single-page-applications (SPA) are exactly that - a single page with all the content loaded as required. In a SPA, Rails has a very small part to play in routing - the Rails server simply receives the URL and loads the SPA, passing the URL down into the SPA for the client-side router to decide which code to run.
61+
62+
### Server side routing
63+
64+
After you have installed Hyperstack, if you look in `routes.rb` you will find this code:
65+
66+
```ruby
67+
get '/(*others)', to: 'hyperstack#app'
68+
```
69+
70+
The code above (at the end of routes.rb) simply routes everything that has not already been routed to the `hyperstack_controller` and runs the `app` method. The `hyperstack_controller` is added dynamically by Hyperstack, and actually you can route to any Controller and method you choose.
71+
72+
### Client side routing
73+
74+
Lets change the default App component to add a basic router:
75+
76+
```ruby
77+
class App < HyperComponent
78+
include Hyperstack::Router
79+
80+
render(DIV) do
81+
Switch do
82+
Route("/", exact: true, mounts: Home)
83+
Route("/docs", exact: true, mounts: Docs)
84+
end
85+
end
86+
end
87+
88+
class Home < HyperComponent
89+
render(DIV) do
90+
H1 { "Home" }
91+
end
92+
end
93+
94+
class Docs < HyperComponent
95+
render(DIV) do
96+
H1 { "Docs" }
97+
end
98+
end
99+
```
100+
101+
Navigate to `localhost:5000/` and the `Home` Component will be rendered and navigate to `localhost:5000/docs` and the `Docs` Component will be rendered.
102+
103+
Now we know the SPA will route to the correct Component when the page first loads. Time for us to add a menu system for some client-side routing (which will not call the server):
104+
105+
```ruby
106+
class App < HyperComponent
107+
include Hyperstack::Router
108+
109+
render(DIV) do
110+
Menu()
111+
Switch do
112+
Route("/", exact: true, mounts: Home)
113+
Route("/docs", exact: true, mounts: Docs)
114+
end
115+
end
116+
end
117+
118+
class Menu < HyperComponent
119+
include Hyperstack::Router::Helpers
120+
121+
render(DIV) do
122+
Link("/") { "Home" }
123+
SPAN { " | " }
124+
Link("/docs") { "Docs" }
125+
end
126+
end
127+
128+
class Home < HyperComponent
129+
render(DIV) do
130+
H1 { "Home" }
131+
end
132+
end
133+
134+
class Docs < HyperComponent
135+
render(DIV) do
136+
H1 { "Docs" }
137+
end
138+
end
139+
```
140+
141+
In the code above, we have added a `Menu` Component with a very basic menu. Using the `Link` helper method from the `include Hyperstack::Router::Helpers` mixin.

0 commit comments

Comments
 (0)