Skip to content

Commit ac16725

Browse files
committed
Merge branch 'develop' into implementing_issue_153
2 parents fb7f28e + 759f9fe commit ac16725

File tree

10 files changed

+242
-5
lines changed

10 files changed

+242
-5
lines changed

app/concepts/matestack/ui/core/async/async.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ const componentDef = {
5858
}
5959
}
6060
}
61+
if(this.componentConfig["init_show"] == true){
62+
this.showing = true
63+
}
6164
},
6265
beforeDestroy: function() {
6366
const self = this

docs/components/async.md

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ end
2626

2727
**Note:** The `rerender_on` option lets you rerender parts of your UI asynchronously, which is cool. But please consider that, if not configured differently, it a) is **not** _lazily loaded_ and b) does get displayed on initial pageload.
2828

29-
Lazy (or defered) loading is a feature we're working on right now, for details see [here](https://github.com/basemate/matestack-ui-core/issues/58).
29+
Lazy (or defered) loading can be configured like shown [here](#defer).
3030

31-
If you want to hide the async component on initial pageload and display it later on, read below as this option (`show_on`) is already implemented (and can be combined with `rerender_on`)!
31+
If you want to simply hide the async component on initial pageload and display it later on, read below how to use `show_on`, which can be combined with `rerender_on`.
3232

3333
### Show_on
3434

35-
The `show_on` option lets us define an event on which the component gets shown.
35+
The `show_on` option lets us define an event on which the component gets shown. The content is still rendered on init pageload, but simply hidden in the browser until the event is emitted. If you want to have proper deferred loading, please refer to [defer](#defer)
3636

3737
```ruby
3838
async show_on: 'my_event' do
@@ -66,6 +66,30 @@ async hide_after: 1000 do
6666
end
6767
```
6868

69+
### Shown_on/Hide_on Combination
70+
71+
If you combine `shown_on` and `hide_on`, you can toggel the view state of the `async` component explicitly.
72+
73+
By default, the content is initially hidden until the show event is emitted when `shown_on` is applied.
74+
75+
```ruby
76+
async shown_on: "my_show_event", hide_on: 'my_hide_event' do
77+
div id: 'my-div' do
78+
plain 'You will not see me after the event'
79+
end
80+
end
81+
```
82+
83+
If you want to display the content initially, simply add `init_show: true`
84+
85+
```ruby
86+
async shown_on: "my_show_event", hide_on: 'my_hide_event', init_show: true do
87+
div id: 'my-div' do
88+
plain 'You will not see me after the event'
89+
end
90+
end
91+
```
92+
6993
### Defer
7094

7195
The `defer` option may be used in two ways:
@@ -203,6 +227,69 @@ end
203227

204228
As expected, the timestamp is only visible _before_ our event was fired and is hidden/invisible _after_ the event!
205229

230+
### Example 3.1: Rerender/Show/Hide Combination for inline editing
231+
232+
Imagine, you want to create an inline edit form. Using a combination of `show_on`, `hide_on`, `init_show:true` and `rerender_on` allows you to implement this easily like shown below:
233+
234+
235+
```ruby
236+
class ExamplePage < Matestack::Ui::Page
237+
238+
def prepare
239+
@my_model = MyModel.find(42)
240+
end
241+
242+
def response
243+
components {
244+
partial :show_value
245+
partial :show_form
246+
}
247+
end
248+
249+
def show_value
250+
partial {
251+
async rerender_on: "item_updated", show_on: "item_updated", hide_on: "update_item", init_show: true do
252+
onclick emit: "update_item" do
253+
plain @my_model.title
254+
end
255+
end
256+
}
257+
end
258+
259+
def show_form
260+
partial {
261+
async rerender_on: "item_updated", show_on: "update_item", hide_on: "item_updated" do
262+
form some_form_config, :include do
263+
form_input key: :title, type: :text
264+
form_submit do
265+
button text: "save"
266+
end
267+
end
268+
onclick emit: "item_updated" do
269+
button text: "abort"
270+
end
271+
end
272+
}
273+
end
274+
275+
def some_form_config
276+
{
277+
for: @my_model,
278+
method: :post,
279+
path: :inline_form_action_path, #or any other rails route
280+
params: {
281+
id: @my_model.id
282+
},
283+
success:{
284+
emit: "item_updated"
285+
}
286+
}
287+
end
288+
289+
end
290+
```
291+
If you click on the attribute value, the form gets shown. After successful form submission, the form gets hidden again and only the simple value is shown.
292+
206293
### Example 4: Hide after show on event
207294

208295
On our example page, we wrap a simple timestamp in an async component and tell it to show up when the event `my_event` gets triggered and be hidden after 1000ms.

spec/dummy/app/controllers/my_app_controller.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def collection
3030
responder_for(Pages::MyApp::Collection)
3131
end
3232

33+
def inline_edit
34+
responder_for(Pages::MyApp::InlineEdit)
35+
end
36+
3337
def some_action
3438
render json: {}, status: :ok
3539
end
@@ -47,6 +51,20 @@ def form_action
4751
end
4852
end
4953

54+
def inline_form_action
55+
@dummy_model = DummyModel.find(params[:id])
56+
@dummy_model.update(dummy_model_params)
57+
if @dummy_model.errors.any?
58+
render json: {
59+
errors: @dummy_model.errors,
60+
message: "Test Model could not be saved!"
61+
}, status: :unproccessable_entity
62+
else
63+
broadcast "test_model_created"
64+
render json: @dummy_model, status: :created
65+
end
66+
end
67+
5068
def delete_dummy_model
5169
@dummy_model = DummyModel.find(params[:id])
5270
if @dummy_model.destroy

spec/dummy/app/matestack/apps/my_app.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def navigation_section
3939
transition path: :collection_path do
4040
button text: "Collection"
4141
end
42+
transition path: :inline_edit_path do
43+
button text: "Inline Edit"
44+
end
4245
end
4346
}
4447
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Pages::MyApp::InlineEdit < Matestack::Ui::Page
2+
3+
def prepare
4+
@my_model = DummyModel.last
5+
end
6+
7+
def response
8+
components {
9+
heading size: 2, text: "Inline Edit"
10+
11+
partial :show_value
12+
partial :show_form
13+
}
14+
end
15+
16+
def show_value
17+
partial {
18+
async rerender_on: "item_updated", show_on: "item_updated", hide_on: "update_item", init_show: true do
19+
onclick emit: "update_item" do
20+
plain @my_model.title
21+
plain "(click me)"
22+
end
23+
end
24+
}
25+
end
26+
27+
def show_form
28+
partial {
29+
async rerender_on: "item_updated", show_on: "update_item", hide_on: "item_updated" do
30+
form some_form_config, :include do
31+
form_input key: :title, type: :text
32+
form_submit do
33+
button text: "save"
34+
end
35+
end
36+
onclick emit: "item_updated" do
37+
button text: "abort"
38+
end
39+
end
40+
}
41+
end
42+
43+
def some_form_config
44+
{
45+
for: @my_model,
46+
method: :post,
47+
path: :inline_form_action_path,
48+
params: {
49+
id: @my_model.id
50+
},
51+
success:{
52+
emit: "item_updated"
53+
}
54+
}
55+
end
56+
57+
58+
end

spec/dummy/app/matestack/pages/my_app/my_first_page.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
class Pages::MyApp::MyFirstPage < Matestack::Ui::Page
22

3+
def prepare
4+
@my_model = DummyModel.last
5+
end
6+
37
def response
48
components {
59
heading size: 2, text: "This is Page 1"

spec/dummy/config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
get 'my_fifth_page', to: 'my_app#my_fifth_page'
2828
get 'my_sixth_page', to: 'my_app#my_sixth_page'
2929
get 'collection', to: 'my_app#collection'
30+
get 'inline_edit', to: 'my_app#inline_edit'
3031

3132
post 'some_action', to: 'my_app#some_action'
3233
post 'form_action', to: 'my_app#form_action'
34+
post 'inline_form_action/:id', to: 'my_app#inline_form_action', as: "inline_form_action"
3335

3436
delete 'delete_dummy_model', to: 'my_app#delete_dummy_model'
3537
end

spec/usage/components/async_spec.rb

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,66 @@ def response
143143
expect(page).not_to have_selector "#my-div"
144144
end
145145

146-
it "Example 3 - hide after show on event" do
146+
it "Example 3.1 - Show on / Hide on combination init not shown by default" do
147+
148+
class ExamplePage < Matestack::Ui::Page
149+
150+
def response
151+
components {
152+
async show_on: "my_show_event", hide_on: "my_hide_event" do
153+
div id: "my-div" do
154+
plain "#{DateTime.now.strftime('%Q')}"
155+
end
156+
end
157+
}
158+
end
159+
160+
end
161+
162+
visit "/example"
163+
164+
expect(page).not_to have_selector "#my-div"
165+
166+
page.execute_script('MatestackUiCore.matestackEventHub.$emit("my_show_event")')
167+
168+
expect(page).to have_selector "#my-div"
169+
170+
page.execute_script('MatestackUiCore.matestackEventHub.$emit("my_hide_event")')
171+
172+
expect(page).not_to have_selector "#my-div"
173+
end
174+
175+
it "Example 3.2 - Show on / Hide on combination init shown if configured" do
176+
177+
class ExamplePage < Matestack::Ui::Page
178+
179+
def response
180+
components {
181+
async show_on: "my_show_event", hide_on: "my_hide_event", init_show: true do
182+
div id: "my-div" do
183+
plain "#{DateTime.now.strftime('%Q')}"
184+
end
185+
end
186+
}
187+
end
188+
189+
end
190+
191+
visit "/example"
192+
193+
expect(page).to have_selector "#my-div"
194+
195+
page.execute_script('MatestackUiCore.matestackEventHub.$emit("my_hide_event")')
196+
197+
expect(page).not_to have_selector "#my-div"
198+
199+
page.execute_script('MatestackUiCore.matestackEventHub.$emit("my_show_event")')
200+
201+
expect(page).to have_selector "#my-div"
202+
end
203+
204+
205+
it "Example 3.3 - hide after show on event" do
147206

148207
class ExamplePage < Matestack::Ui::Page
149208

vendor/assets/javascripts/matestack-ui-core.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/assets/javascripts/matestack-ui-core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)