You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Run the installer (which will create all the Hyperstack folders and config)
30
30
31
-
```
31
+
```bash
32
32
bundle exec rails hyperstack:install
33
33
```
34
34
35
35
And that's it, Hyperstack is installed.
36
36
37
37
Run the server and navigate load the webpage in your browser:
38
38
39
-
```
39
+
```bash
40
40
foreman start
41
41
```
42
42
43
-
Navigate to:
43
+
Navigate to:
44
44
45
45
`http://localhost:5000/`
46
46
@@ -50,8 +50,92 @@ You should see a page with the word `App` - hooray you are installed!
50
50
51
51
+`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).
52
52
+`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.
54
54
+`initializers\hyperstack.rb` contains all the Hyperstack optional initialization.
55
55
+`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.
56
56
+`package.json` is used by Webpacker and Yarn (basically like a gemfile) to specify which JS libraries will be required.
57
57
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
+
classApp < HyperComponent
78
+
includeHyperstack::Router
79
+
80
+
render(DIV) do
81
+
Switchdo
82
+
Route("/", exact:true, mounts:Home)
83
+
Route("/docs", exact:true, mounts:Docs)
84
+
end
85
+
end
86
+
end
87
+
88
+
classHome < HyperComponent
89
+
render(DIV) do
90
+
H1 { "Home" }
91
+
end
92
+
end
93
+
94
+
classDocs < 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
+
classApp < HyperComponent
107
+
includeHyperstack::Router
108
+
109
+
render(DIV) do
110
+
Menu()
111
+
Switchdo
112
+
Route("/", exact:true, mounts:Home)
113
+
Route("/docs", exact:true, mounts:Docs)
114
+
end
115
+
end
116
+
end
117
+
118
+
classMenu < HyperComponent
119
+
includeHyperstack::Router::Helpers
120
+
121
+
render(DIV) do
122
+
Link("/") { "Home" }
123
+
SPAN { " | " }
124
+
Link("/docs") { "Docs" }
125
+
end
126
+
end
127
+
128
+
classHome < HyperComponent
129
+
render(DIV) do
130
+
H1 { "Home" }
131
+
end
132
+
end
133
+
134
+
classDocs < 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