Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit f14d895

Browse files
authored
Update README.md
1 parent a58c98e commit f14d895

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ Notice that we did not create any APIs to achieve this. Data on the server is s
148148
Now that we have all of our pieces in place, lets build our application.
149149

150150
Replace the entire contents of `app.rb` with:
151+
151152
```ruby
152153
# app/hyperloop/components/app.rb
153154
class App < Hyperloop::Component
@@ -162,6 +163,7 @@ end
162163
The browser screen will go blank because we have not defined the three subcomponents. Lets define them now:
163164

164165
Add three new ruby files to the `app/hyperloop/components` folder:
166+
165167
```ruby
166168
# app/hyperloop/components/header.rb
167169
class Header < Hyperloop::Component
@@ -170,6 +172,7 @@ class Header < Hyperloop::Component
170172
end
171173
end
172174
```
175+
173176
```ruby
174177
# app/hyperloop/components/index.rb
175178
class Index < Hyperloop::Component
@@ -178,6 +181,7 @@ class Index < Hyperloop::Component
178181
end
179182
end
180183
```
184+
181185
```ruby
182186
# app/hyperloop/components/footer.rb
183187
class Footer < Hyperloop::Component
@@ -186,6 +190,7 @@ class Footer < Hyperloop::Component
186190
end
187191
end
188192
```
193+
189194
Once you add the Footer component you should see:
190195

191196
<div style="border:solid; margin-left: 10px; padding: 10px">
@@ -202,6 +207,7 @@ Notice how the usual HTML tags such as DIV, SECTION, and HEADER are all availabl
202207
### Chapter 4: Listing the Todos, HyperReact Params, and Prerendering
203208

204209
To display each Todo we will create a TodoItem component that takes a parameter:
210+
205211
```ruby
206212
# app/hyperloop/components/todo_item.rb
207213
class TodoItem < Hyperloop::Component
@@ -211,7 +217,9 @@ class TodoItem < Hyperloop::Component
211217
end
212218
end
213219
```
220+
214221
We can use this component in our Index component:
222+
215223
```ruby
216224
# app/hyperloop/components/index.rb
217225
class Index < Hyperloop::Component
@@ -224,6 +232,7 @@ class Index < Hyperloop::Component
224232
end
225233
end
226234
```
235+
227236
Now you will see something like
228237

229238
<div style="border:solid; margin-left: 10px; padding: 10px">
@@ -264,6 +273,7 @@ class TodoItem < Hyperloop::Component
264273
end
265274
end
266275
```
276+
267277
Now your display should look like this:
268278
<div style="border:solid; margin-left: 10px; padding: 10px">
269279
<div>Header will go here</div>
@@ -304,6 +314,7 @@ It reads like a good novel doesn't it? On a `click` event update the todo, sett
304314
Meanwhile HyperReact sees the value of `params.todo.checked` changing, and this causes the associated HTML INPUT tag to be re-rendered.
305315

306316
We will finish up by adding a delete link at the end of the Todo item:
317+
307318
```ruby
308319
# app/hyperloop/components/todo_item.rb
309320
class TodoItem < Hyperloop::Component
@@ -316,6 +327,7 @@ class TodoItem < Hyperloop::Component
316327
end
317328
end
318329
```
330+
319331
*Note: If a component or tag block returns a string it is automatically wrapped in a SPAN, to insert a string in the middle you have to wrap it a SPAN like we did above.*
320332

321333
I hope you are starting to see a pattern here. HyperReact components determine what to display based on the `state` of some
@@ -339,6 +351,7 @@ class Todo < ApplicationRecord
339351
scope :active, -> () { where(completed: false) }
340352
end
341353
```
354+
342355
Now we can say `Todo.all`, `Todo.completed`, and `Todo.active`, and get the desired subset of Todos.
343356
You might want to try it now in the rails console. *Note: you will have to do a `reload!` to load the changes to the Model.*
344357

@@ -352,6 +365,7 @@ We would like the URL of our App to reflect which of these *filters* is being di
352365
Having the application display different data (or whole different components) based on the URL is called routing.
353366

354367
Lets change `App` to look like this:
368+
355369
```ruby
356370
# app/hyperloop/components/app.rb
357371
class App < Hyperloop::Router
@@ -367,6 +381,7 @@ class App < Hyperloop::Router
367381
end
368382
```
369383
and the `Index` component to look like this:
384+
370385
```ruby
371386
# app/hyperloop/components/index.rb
372387
class Index < Hyperloop::Router::Component
@@ -409,6 +424,7 @@ This is telling Rails to accept all requests and to process them using the `hype
409424
### Chapter 7: Helper Methods, Inline Styling, Active Support and Router Nav Links
410425

411426
Of course we will want to add navigation to move between these routes. We will put the navigation in the footer:
427+
412428
```ruby
413429
# app/hyperloop/components/footer.rb
414430
class Footer < Hyperloop::Component
@@ -437,10 +453,12 @@ To make this happen we will *mixin* some router helpers by *including* `HyperRou
437453
Then we can replace the anchor tag with the Router's `NavLink` component:
438454

439455
Change
456+
440457
```ruby
441458
A(href: "/#{path}", style: {marginRight: 10}) { path.camelize }
442459
```
443460
to
461+
444462
```ruby
445463
NavLink("/#{path}", style: {marginRight: 10}) { path.camelize }
446464
```
@@ -470,6 +488,7 @@ Associated with this history is a (you guessed it I hope) *state*. So when the
470488
So far we can mark Todos as completed, delete them, and filter them. Now we create an `EditItem` component so we can change the Todo title.
471489

472490
Add a new component like this:
491+
473492
```ruby
474493
# app/hyperloop/components/edit_item.rb
475494
class EditItem < Hyperloop::Component
@@ -489,10 +508,12 @@ Before we use this component let's understand how it works.
489508
+ When the user types the enter key (key code 13) the todo is saved.
490509

491510
Now update the `TodoItem` component replacing
511+
492512
```ruby
493513
SPAN { params.todo.title }
494514
```
495515
with
516+
496517
```ruby
497518
EditItem(todo: params.todo)
498519
```
@@ -510,6 +531,7 @@ To summarize:
510531
+ User changes focus away (`blur`) from the Todo being edited: editing changes to `false`.
511532
In order to accomplish this our `EditItem` component is going to communicate via two callbacks - `on_save` and `on_cancel` - with the parent component. We can think of these callbacks as custom events, and indeed as we shall see they will work just like any other event.
512533
Add the following 5 lines to the `EditItem` component like this:
534+
513535
```ruby
514536
# app/hyperloop/components/edit_item.rb
515537
class EditItem < Hyperloop::Component
@@ -543,6 +565,7 @@ For example, if we had left off `type: Proc` we would have to say `params.on_sav
543565
Finally we add the `blur` event handler and simply transform it into our custom `cancel` event.
544566

545567
Now we can update our `TodoItem` component to be a little state machine, which will react to three events: `double_click`, `save` and `cancel`.
568+
546569
```ruby
547570
# app/hyperloop/components/todo_item.rb
548571
class TodoItem < Hyperloop::Component
@@ -586,6 +609,7 @@ Our EditItem component has a good robust interface. It takes a Todo, and lets t
586609
Because of this we can easily reuse EditItem to create new Todos. Not only does this save us time, but it also insures that the user interface acts consistently.
587610

588611
Update the `Header` component to use EditItem like this:
612+
589613
```ruby
590614
# app/hyperloop/components/header.
591615
class Header < Hyperloop::Component
@@ -637,6 +661,7 @@ You will have to refresh the page after changing the style sheet.
637661
Now its a matter of updating the css classes which are passed to components via the `class` parameter.
638662

639663
Let's start with the `App` component. With styling it will look like this:
664+
640665
```ruby
641666
# app/hyperloop/components/app.rb
642667
class App < Hyperloop::Router
@@ -652,6 +677,7 @@ end
652677
```
653678
The `Footer` components needs have a `UL` added to hold the links nicely,
654679
and we can also use the `NavLinks` `active_class` param to highlight the link that is currently active:
680+
655681
```ruby
656682
# app/hyperloop/components/footer.rb
657683
class Footer < Hyperloop::Component
@@ -672,6 +698,7 @@ class Footer < Hyperloop::Component
672698
end
673699
```
674700
For the Index component just add the `main` and `todo-list` classes.
701+
675702
```ruby
676703
# app/hyperloop/components/index.rb
677704
class Index < Hyperloop::Router::Component
@@ -686,6 +713,7 @@ end
686713
```
687714
For the EditItem component we want the caller specify the class. To keep things compatible with React.js we need to call the param `className`,
688715
but we can still send it to EditItem with the usual hyperloop style `class` param.
716+
689717
```ruby
690718
# app/hyperloop/components/edit_item.rb
691719
class EditItem < Hyperloop::Component
@@ -710,6 +738,7 @@ class EditItem < Hyperloop::Component
710738
end
711739
```
712740
Now we can add classes to the TodoItem's list-item, input, anchor tags, and to the `EditItem` component:
741+
713742
```ruby
714743
# app/hyperloop/components/todo_item.rb
715744
class TodoItem < Hyperloop::Component
@@ -732,6 +761,7 @@ end
732761
```
733762
In the Header we can send a different class to the `EditItem` component. While we are at it
734763
we will add the `H1 { 'todos' }` hero unit.
764+
735765
```ruby
736766
# app/hyperloop/components/header.rb
737767
class Header < Hyperloop::Component
@@ -748,7 +778,8 @@ At this point your Todo App should be properly styled.
748778
### Chapter 12: Other Features
749779

750780
+ **Show How Many Items Left In Footer**
751-
This is just a span that we add before the link tags list in the Footer component:
781+
This is just a span that we add before the link tags list in the Footer component:
782+
752783
```ruby
753784
...
754785
render(DIV, class: :footer) do
@@ -760,6 +791,7 @@ This is just a span that we add before the link tags list in the Footer componen
760791
```
761792
+ **Add 'placeholder' Text To Edit Item**
762793
EditItem should display a meaningful placeholder hint if the title is blank:
794+
763795
```ruby
764796
...
765797
INPUT(
@@ -772,6 +804,7 @@ EditItem should display a meaningful placeholder hint if the title is blank:
772804
```
773805
+ **Don't Show the Footer If There are No Todos**
774806
In the `App` component add a *guard* so that we won't show the Footer if there are no Todos:
807+
775808
```ruby
776809
...
777810
Footer() unless Todo.count.zero?

0 commit comments

Comments
 (0)