Cable Component API
The cable component enables us to update the DOM based on events and data pushed via ActionCable without a browser reload.
Please read the ActionCable Guide if you need help setting up ActionCable for your project, and make sure you have set up ActionCable correctly. The following code snippet is crucial to make the cable component work correctly:
app/javascript/channels/matestack_ui_core_channel.js
1
import MatestackUiVueJs form 'matestack-ui-core';
2
3
consumer.subscriptions.create("MatestackUiVueJsChannel", {
4
//...
5
received(data) {
6
MatestackUiVueJs.eventHub.$emit(data.event, data)
7
}
8
});
Copied!
A cable component renders a Vue.js driven cable component initially containing content specified by a block.
Imagine something like this:
1
class Page < Matestack::Ui::Page
2
3
def response
4
cable id: 'foo' [...] do
5
# this block will be rendered as initial content and may be
6
# modified on the client side later upon receiving defined events
7
DummyModel.all.each do |instance|
8
ListComponent.call(item: instance)
9
end
10
end
11
end
12
end
Copied!
1
class ListComponent < Matestack::Ui::Component
2
3
required :item
4
5
def response
6
# make sure to define an unique ID on the root element of your component
7
# the declared ID may be used later on to update or delete this component on the client side
8
div id: "item-#{context.item.id}", class: "row" do
9
#...
10
end
11
end
12
13
end
Copied!
Please note: When rendering a list of items, we recommend to use a custom component for each item. This makes it easy to render unique items on the server side and push them via ActionCable to the client. Technically, it is also possible to use another component or a simple html string. Any given html will be used to update the item.

Parameters

id - required

Expects an unique string that identifies the cable component

append_on

Expects a string that matches the event which will be emitted via ActionCable on the serverside. Event payload data in form of HTML will be appended to the current cable component DOM.
In your app, page or component:
1
cable id: 'foo', append_on: "model_created" do
2
DummyModel.all.each do |instance|
3
ListComponent.call(item: instance)
4
end
5
end
Copied!
In your controller:
1
ActionCable.server.broadcast("matestack_ui_core", {
2
event: "model_created",
3
data: ListComponent.call(item: @new_model_instance)
4
})
Copied!
data can also be an array of components.

prepend_on

Expects a string that matches the event which will be emitted via ActionCable on the serverside. Event payload data in form of HTML will be prepended to the current cable component DOM.
In your app, page or component:
1
cable id: 'foo', prepend_on: "model_created" do
2
DummyModel.all.each do |instance|
3
ListComponent.call(item: instance)
4
end
5
end
Copied!
In your controller:
1
ActionCable.server.broadcast("matestack_ui_core", {
2
event: "model_created",
3
data: ListComponent.call(item: @new_model_instance)
4
})
Copied!
data can also be an array of components.

replace_on

Expects a string that matches the event which will be emitted via ActionCable on the serverside. Event payload data in form of HTML will replace the whole current cable component DOM.
In your app, page or component:
1
cable id: 'foo', replace_on: "model_created" do
2
DummyModel.all.each do |instance|
3
ListComponent.call(item: instance)
4
end
5
end
Copied!
In your controller:
1
ActionCable.server.broadcast("matestack_ui_core", {
2
event: "model_created",
3
data: ListComponent.call(item: @new_model_instance)
4
})
Copied!
data can also be an array of components.

update_on

Expects a string that matches the event which will be emitted via ActionCable on the serverside. Event payload data in form of HTML will update a specific element iditified by its root ID within the current cable component DOM.
In your app, page or component:
1
cable id: 'foo', append_on: "model_created" do
2
DummyModel.all.each do |instance|
3
ListComponent.call(item: instance)
4
end
5
end
Copied!
In your controller:
1
ActionCable.server.broadcast("matestack_ui_core", {
2
event: "model_created",
3
data: matestack_component(:list_component, item: @new_model_instance)
4
})
Copied!
data can also be an array of components.

delete_on

Expects a string that matches the event which will be emitted via ActionCable on the serverside. Event payload data in form of a string containing the ID will remove a specific element identified by its root ID within the current cable component DOM.
In your app, page or component:
1
cable id: 'foo', delete_on: "model_deleted" do
2
DummyModel.all.each do |instance|
3
ListComponent.call(item: instance)
4
end
5
end
Copied!
In your controller:
1
ActionCable.server.broadcast("matestack_ui_core", {
2
event: "model_deleted",
3
data: "item-#{params[:id]}"
4
})
Copied!
data can also be an Array of ID-strings.

Examples

Imagine a list of Todo Items and a above that list a form to create new Todos, implemented like this:
1
class MyPage < Matestack::Ui::Page
2
3
def response
4
matestack_form method: :post, path: create_action_rails_route_path do
5
form_input key: :title, type: :text
6
button "submit"
7
end
8
9
Todo.all.each do |instance|
10
TodoComponent.call(todo: instance)
11
end
12
end
13
14
end
Copied!
1
class TodoComponent < Matestack::Ui::Component
2
3
required :todo
4
5
def response
6
div id: "todo-#{context.todo.id}", class: "row" do
7
div class: "col" do
8
plain context.todo.title
9
end
10
end
11
end
12
13
end
Copied!
After form submission, the form resets itself dynamically, but the list will not get updated with the new todo instance. You can now decide, if you want to use async or cable in order to implement that reactivity. async could react to an event emitted by the form and simply rerender the whole list without any further implementation. Wrapping the list in a correctly configured async component would be enough!
But in this case, we do not want to rerender the whole list every time we submitted the form, because - let's say - the list will be quite long and rerendering the whole list would be getting slow. We only want to add new items to the current DOM without touching the rest of the list. The cable component enables you to do exactly this. The principle behind it: After form submission the new component is rendered on the serverside and than pushed to the clientside via ActionCable. The cable component receives this event and will than - depending on your configuration - append or prepend the current list on the UI. Implementation would look like this:

Appending elements

adding elements on the bottom of the list
1
class MyPage < Matestack::Ui::Page
2
3
def response
4
matestack_form method: :post, path: create_action_rails_route_path do
5
form_input key: :title, type: :text
6
button "submit"
7
end
8
9
cable id: "my-cable-list", append_on: "new_todo_created" do
10
Todo.all.each do |instance|
11
TodoComponent.call(todo: instance)
12
end
13
end
14
end
15
16
end
Copied!
and on your controller:
1
def create
2
@todo = Todo.create(todo_params)
3
4
unless @todo.errors.any?
5
ActionCable.server.broadcast("matestack_ui_core", {
6
event: "new_todo_created",
7
data: TodoComponent.call(todo: @todo)
8
})
9
# respond to the form POST request (needs to be last)
10
render json: { }, status: :created
11
end
12
end
Copied!
Please notice that we recommend to use a component for each list item. With a component for each item it is possible to easily render a new list item within the create action and push it to the client. But it is possible to also use another component or a html string. Any given html will be appended to the list.

Prepending elements

adding elements on the top of the list
Prepending works pretty much the same as appending element, just configure your cable component like this:
1
cable id: "my-cable-list", prepend_on: "new_todo_created" do
2
Todo.all.each do |instance|
3
TodoComponent.call(todo: instance)
4
end
5
end
Copied!

Updating elements

updating existing elements within the list
Now imagine you want to update elements in your list without browser reload because somewhere else the title of a todo instance was changed. You could use async for this as well. Esspecially because async can react to serverside events pushed via ActionCable as well. But again: async would rerender the whole list... and in our usecase we do not want to this. We only want to update a specific element of the list. Luckily the implementation for this features does not differ from the above explained ones!
Imagine somewhere else the specific todo was updated via a form targeting the following controller action:
1
def update
2
@todo = Todo.find(params[:id])
3
@todo.update(todo_params)
4
5
unless @todo.errors.any?
6
ActionCable.server.broadcast("matestack_ui_core", {
7
event: "todo_updated",
8
data: TodoComponent.call(todo: @todo)
9
})
10
# respond to the form PUT request (needs to be last)
11
render json: { }, status: :ok
12
end
13
end
Copied!
Again, the controller action renders a new version of the component and pushes that to the clientside. Nothing changed here! We only need to tell the cable component to react properly to that event:
1
cable id: "my-cable-list", update_on: "todo_updated" do
2
Todo.all.each do |instance|
3
TodoComponent.call(todo: instance)
4
end
5
end
Copied!
Please notice that it is mandatory to have a unique ID on the root element of each list item. The cable component will use the ID found in the root element of the pushed component in order to figure out, which element of the current list should be updated. In our example above we used div id: "todo-#{todo.id}" as the root element of our todo_component used for each element in the list.

Removing elements

removing existing elements within the list
Well, of course we want to be able to remove elements from that list without rerendering the whole list, as async would do. The good thing: We can tell the cable component to delete elements by ID:
1
cable id: "my-cable-list", delete_on: "todo_deleted" do
2
Todo.all.each do |instance|
3
TodoComponent.call(todo: instance)
4
end
5
end
Copied!
Imagine somewhere else the following destroy controller action was targeted:
1
def destroy
2
@todo = Todo.find(params[:id])
3
4
if @todo.destroy
5
ActionCable.server.broadcast("matestack_ui_core", {
6
event: "todo_deleted",
7
data: "todo-#{params[:id]}"
8
})
9
# respond to the DELETE request (needs to be last)
10
render json: { }, status: :deleted
11
end
12
end
Copied!
After deleting the todo instance, the controller action pushes an event via ActionCable, now including just the ID of the element which should be removed. Notice that this ID have to match the ID used on the root element of the component. In our example above we used div id: "todo-#{todo.id}" as the root element of our todo_component used for each element in the list.

Replacing the whole component body

Now imagine a context in which the whole cable component body should be updated rather than just adding/updating/deleting specific elements of a list. In an online shop app this could be the shopping cart component rendered on the top right. When adding a product to the cart, you might want to update the shopping cart component in order to display the new amount of already included products.
The component may look like this:
1
class ShoppingCart < Matestack::Ui::Component
2
3
def response
4
div id: "shopping-cart", class: "some-fancy-styling" do
5
icon "some-shopping-cart-icon"
6
span count, class: "some-badge"
7
transition path: shopping_cart_path do
8
button "to my cart"
9
end
10
end
11
end
12
13
def count
14
# some logic returning the amount of products in the users cart (saved on serverside)
15
current_user.cart.products_count
16
end
17
18
end
Copied!
Imagine somewhere else the following controller action was targeted when adding a product to the cart:
1
def add_to_cart
2
@product = Product.find(params[:id])
3
current_user.cart.add(@product) #some logic adding the product to the users cart (saved on serverside)
4
5
ActionCable.server.broadcast("matestack_ui_core", {
6
event: "shopping_cart_updated",
7
data: ShoppingCart.call()
8
})
9
render json: { }, status: :ok
10
end
Copied!
and on your UI class (probably your app class):
1
cable id: "shopping-cart", replace_on: "shopping_cart_updated" do
2
ShoppingCart.call()
3
end
Copied!

Event data as Array

All above shown examples demonstrated how to push a single component or ID to the cable component. In all usecases it's also possble to provide an Array of components/ID-strings, e.g.:
1
ActionCable.server.broadcast("matestack_ui_core", {
2
event: "todo_updated",
3
data: [
4
ShoppingCart.call(todo: @todo1),
5
ShoppingCart.call(todo: @todo2)
6
]
7
})
Copied!