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
A cable
component renders a Vue.js driven cable component initially containing content specified by a block.
Imagine something like this:
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:
In your controller:
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:
In your controller:
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:
In your controller:
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:
In your controller:
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:
In your controller:
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:
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
and on your controller:
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:
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:
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:
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:
Imagine somewhere else the following destroy controller action was targeted:
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:
Imagine somewhere else the following controller action was targeted when adding a product to the cart:
and on your UI class (probably your app class):
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.:
Last updated