Skip to content

Commit ec28942

Browse files
author
Nils Henning
committed
[TASK][REFACTOR] rework vue.js components guide
1 parent 02f42ea commit ec28942

File tree

1 file changed

+3
-55
lines changed

1 file changed

+3
-55
lines changed

docs/guides/essential/09_custom_vue_js_components.md

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,13 @@ Welcome to the ninth part of our essential guide about building a web applicatio
77
In a [previous guide](/docs/guides/essential/07_partials_and_custom_components.md), we introduced custom components. In this one, we're bringing it to the next level with custom vue.js components!
88

99
In this guide, we will
10-
- add a custom vue.js component that fetches and displays data from a 3rd-party API
10+
- add a custom vue.js component that fetches and displays data from a third-party API
1111

1212
## Prerequisites
1313

1414
We expect you to have successfully finished the [previous guide](guides/essential/06_static_components.md).
1515

16-
## Hiding content on button click
17-
18-
Remember the `disclaimer` custom component from the last part? Let's turn it into a custom dynamic component. For a dynamic component, we need a JavaScript file.
19-
20-
Since we only used the `app/matestack/components/person/disclaimer.haml` file as a showcase, go ahead and replace it with a file called `disclaimer.js` that contains our JavaScript logic:
21-
22-
```javascript
23-
MatestackUiCore.Vue.component('custom-person-disclaimer', {
24-
mixins: [MatestackUiCore.componentMixin],
25-
data() {
26-
return {
27-
show: true
28-
};
29-
}
30-
});
31-
```
32-
33-
Our component only gets one boolean data variable which is initialized as `true`. Let's put it to use by replacing the contents of `app/matestack/components/person/disclaimer.rb` with the following:
34-
35-
```ruby
36-
class Components::Person::Disclaimer < Matestack::Ui::DynamicComponent
37-
38-
def response
39-
div attributes: {"v-show": "show == true"} do
40-
paragraph text: 'None of the presented names belong to and/or are meant to refer to existing human beings. They were created using a "Random Name Generator".'
41-
button attributes: {"@click": "show = false"}, text: 'Hide'
42-
end
43-
end
44-
45-
end
46-
```
47-
48-
All components inside the component's response are wrapped into a Vue.js conditional so they only get shown while the **show**-boolean is set to `true`. Upon clicking the button inside the component's response, this boolean gets set to `false` and therefore the whole component disappears - neat!
49-
50-
The last thing to do is adding the newly created dynamic component to `app/javascript/packs/application.js` so it gets correctly compiled by Webpack:
51-
52-
```javascript
53-
import '../../matestack/components/person/disclaimer'
54-
```
55-
56-
> need to register in component registry?
57-
58-
Start your application and see if it works! You should be able to click the 'Hide'-button below the disclaimer text and it disappears/gets hidden.
59-
60-
Note that it stays hidden when `matestack` page transition get performed, but re-appears on full page reloads. This was expected since the display/hide is performed purely on the client-side and, unlike a cookie or a configuration stored in a database, doesn't persist somewhere.
61-
62-
As usual, we want to commit the progress to Git. In the repo root, run
63-
64-
```sh
65-
git add app/javascript/packs/application.js app/matestack/components/person/ && git commit -m "Refactor disclaimer from static to dynamic custom component"
66-
```
67-
68-
## Fetching and displaying data from 3rd party APIs
16+
## Fetching and displaying data from a third party API
6917

7018
We want to display an option for what you could do with the person at its show page. Therefore we want to fetch a json api and display content of the responses on the page.
7119

@@ -181,7 +129,7 @@ class Demo::Pages::Persons::Show < Matestack::Ui::Page
181129
end
182130
```
183131

184-
Spin up your app and head to the **Show** page to play around - fetching and deleting activities should work flawlessly! Hint: The way we have set it up for now, everything in this component happens on the client side and refreshing the page resets your activities to an empty array.
132+
Spin up your app and head to the **Show** page to play around - fetching and deleting activities should work flawlessly! Hint: The way we have set it up for now, everything in this component happens on the client side and refreshing the page resets your activities to an empty array. If you use page transitions to swap to the index page and then back to the user, the data should still be there.
185133

186134
Again, don't forget to save your progress to Git. In the repo root, run
187135

0 commit comments

Comments
 (0)