Skip to content

Commit fd01222

Browse files
jonasjabariNils Henning
authored andcommitted
[WIP] marked ID as required for async components, added update_on to async components, modified component helper in order to be used within controllers, added demo use case to dummy app
1 parent 682d7d4 commit fd01222

File tree

12 files changed

+119
-23
lines changed

12 files changed

+119
-23
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const componentDef = {
99
data: function(){
1010
return {
1111
asyncTemplate: null,
12+
asyncTemplateDOMelement: null,
1213
showing: true,
1314
loading: false,
1415
hideAfterTimeout: null,
@@ -47,6 +48,29 @@ const componentDef = {
4748
self.rerender()
4849
}, parseInt(this.componentConfig["defer"]));
4950
},
51+
update: function(payload){
52+
let position = "beforeend"
53+
54+
if(this.componentConfig["position"] === "begin"){
55+
position = "afterbegin"
56+
}
57+
if(this.componentConfig["position"] === "end"){
58+
position = "beforeend"
59+
}
60+
61+
if(payload.new_element != undefined){
62+
this.asyncTemplateDOMelement.insertAdjacentHTML(
63+
position,
64+
payload.new_element
65+
)
66+
}
67+
68+
if(payload.remove_element_by_id != undefined){
69+
this.asyncTemplateDOMelement.querySelector('#' + payload.remove_element_by_id).remove()
70+
}
71+
72+
this.asyncTemplate = this.asyncTemplateDOMelement.outerHTML
73+
},
5074
rerender: function(){
5175
var self = this;
5276
self.loading = true;
@@ -69,12 +93,15 @@ const componentDef = {
6993
self.asyncTemplate = template;
7094
})
7195
.catch(function(error){
72-
console.log(error)
7396
matestackEventHub.$emit('async_rerender_error', { id: self.componentConfig["component_key"] })
7497
})
7598
}
7699
},
77100
created: function () {
101+
102+
this.asyncTemplateDOMelement = document.querySelector("#" + this.componentConfig["id"])
103+
this.asyncTemplate = this.asyncTemplateDOMelement.outerHTML;
104+
78105
const self = this
79106
if(this.componentConfig["show_on"] != undefined){
80107
this.showing = false
@@ -89,6 +116,10 @@ const componentDef = {
89116
var rerender_events = this.componentConfig["rerender_on"].split(",")
90117
rerender_events.forEach(rerender_event => matestackEventHub.$on(rerender_event.trim(), self.rerender));
91118
}
119+
if(this.componentConfig["update_on"] != undefined){
120+
var update_events = this.componentConfig["update_on"].split(",")
121+
update_events.forEach(update_event => matestackEventHub.$on(update_event.trim(), self.update));
122+
}
92123
if(this.componentConfig["show_on"] != undefined){
93124
this.showing = false
94125
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Matestack::Ui::Core::Async
22
class Async < Matestack::Ui::Core::Component::Rerender
33
vue_js_component_name "matestack-ui-core-async"
44

5-
optional :id # will be required in 1.0.0
5+
requires :id # will be required in 1.0.0
66

77
def initialize(*args)
88
super

app/helpers/matestack/ui/core/application_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module ApplicationHelper
1414
def self.included(base)
1515
base.extend(ClassMethods)
1616
end
17-
17+
1818
module ClassMethods
1919
def matestack_app _class
2020
@matestack_app_class = _class
@@ -99,7 +99,7 @@ def responder_for(*args)
9999
end
100100

101101
def matestack_component(component, options = {}, &block)
102-
context = (options[:matestack_context] ||= {}).merge(controller: @_controller)
102+
context = (options[:matestack_context] ||= {}).merge(controller: @_controller || self)
103103
Matestack::Ui::Core::Component::Base.new(options.merge(matestack_context: context)).send(component, options.merge(matestack_context: context), &block)
104104
end
105105
end

spec/dummy/app/assets/javascripts/application.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020

2121
App.cable.subscriptions.create("MatestackUiCoreChannel", {
2222
received(data) {
23-
MatestackUiCore.matestackEventHub.$emit('MatestackUiCoreChannel', data)
23+
MatestackUiCore.matestackEventHub.$emit(data.message, data)
2424
}
2525
});

spec/dummy/app/controllers/my_app_controller.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class MyAppController < ApplicationController
22

33
include Matestack::Ui::Core::ApplicationHelper
4-
4+
55
include Components::Registry
66
include Demo::Components::Registry
77

@@ -56,7 +56,10 @@ def form_action
5656
message: "Test Model could not be saved!"
5757
}, status: :unprocessable_entity
5858
else
59-
broadcast "test_model_created"
59+
ActionCable.server.broadcast("matestack_ui_core", {
60+
message: "test_model_created",
61+
new_element: matestack_component(:my_list_item, item: @dummy_model).to_s
62+
})
6063
render json: { transition_to: my_second_page_path }, status: :created
6164
end
6265
end
@@ -78,7 +81,10 @@ def inline_form_action
7881
def delete_dummy_model
7982
@dummy_model = DummyModel.find(params[:id])
8083
if @dummy_model.destroy
81-
broadcast "test_model_deleted"
84+
ActionCable.server.broadcast("matestack_ui_core", {
85+
message: "test_model_deleted",
86+
remove_element_by_id: "item-#{params[:id]}"
87+
})
8288
render json: {}, status: :ok
8389
else
8490
render json: {
@@ -99,9 +105,10 @@ def dummy_model_params
99105
)
100106
end
101107

102-
def broadcast message
108+
def broadcast message, item=nil
103109
ActionCable.server.broadcast("matestack_ui_core", {
104-
message: message
110+
message: message,
111+
new_element: matestack_component(:my_list_item, item: item).to_s
105112
})
106113
end
107114

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class Components::Card < Matestack::Ui::StaticComponent
22

33
def response
4-
components {
5-
div id: "my-component" do
6-
plain "card"
7-
end
8-
}
4+
li id: "my-component" do
5+
plain "card"
6+
end
97
end
108

119
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Components::MyListItem < Matestack::Ui::Component
2+
3+
requires :item
4+
5+
def response
6+
li id: "item-#{item.id}" do
7+
plain item.title
8+
onclick emit: "test" do
9+
button text: "test"
10+
end
11+
end
12+
end
13+
14+
end

spec/dummy/app/matestack/components/registry.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Components::Registry
22

33
Matestack::Ui::Core::Component::Registry.register_components(
44
my_card: Components::Card,
5+
my_list_item: Components::MyListItem,
56
some_card: Components::Card,
67
some_other_card: Components::Card,
78
some_stupid_card: Components::Card,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class Demo::Pages::MyFirstPage < Matestack::Ui::Page
22

33
def prepare
44
@my_model = DummyModel.last
5+
56
end
67

78
def response
@@ -16,6 +17,15 @@ def response
1617
my_demo_card
1718

1819
foo_fancy_stuff title: 'Huhu'
20+
21+
22+
ul do
23+
async update_on: "test_model_created, test_model_deleted", position: "end", id: "my-list" do
24+
DummyModel.all.each do |instance|
25+
my_list_item item: instance
26+
end
27+
end
28+
end
1929
end
2030

2131

vendor/assets/javascripts/dist/manifest.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
"entrypoints": {
33
"matestack-ui-core": {
44
"css": [
5-
"/dist/matestack-ui-core.min.css"
5+
"/dist/matestack-ui-core.css"
66
],
77
"js": [
8-
"/dist/matestack-ui-core.min.js"
8+
"/dist/matestack-ui-core.js"
9+
],
10+
"css.map": [
11+
"/dist/matestack-ui-core.css.map"
912
],
1013
"js.map": [
11-
"/dist/matestack-ui-core.min.js.map"
14+
"/dist/matestack-ui-core.js.map"
1215
]
1316
}
1417
},
15-
"matestack-ui-core.css": "/dist/matestack-ui-core.min.css",
16-
"matestack-ui-core.js": "/dist/matestack-ui-core.min.js",
17-
"matestack-ui-core.js.map": "/dist/matestack-ui-core.min.js.map"
18+
"matestack-ui-core.css": "/dist/matestack-ui-core.css",
19+
"matestack-ui-core.css.map": "/dist/matestack-ui-core.css.map",
20+
"matestack-ui-core.js": "/dist/matestack-ui-core.js",
21+
"matestack-ui-core.js.map": "/dist/matestack-ui-core.js.map"
1822
}

0 commit comments

Comments
 (0)