CableReady
Search…
DOM Mutations

append

Inserts HTML into the DOM, inside the target element, after its last child.
1
append(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
delay: Integer, # [0] - wait for n milliseconds before running
5
focus_selector: String, # [null] - string containing a CSS selector
6
html: String, # [null] - the HTML to assign
7
select_all: Boolean, # [false] - operate on list of elements returned from selector
8
selector: String, # required - string containing a CSS selector or XPath expression
9
xpath: Boolean # [false] - process the selector as an XPath expression
10
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-append
  • cable-ready:after-append

Reference

graft

Reparent the target element and its children to a new parent element in the DOM.
Grafted elements are moved, not recreated. This means that any Stimulus controllers attached to an element will disconnect and then immediatelyconnect again, but their internal state remains intact.
1
graft(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
delay: Integer, # [0] - wait for n milliseconds before running
5
focus_selector: String, # [null] - string containing a CSS selector
6
parent: String, # [null] - string containing a CSS selector
7
select_all: Boolean, # [false] - operate on list of elements returned from selector
8
selector: String, # required - string containing a CSS selector or XPath expression
9
xpath: Boolean # [false] - process the selector as an XPath expression
10
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-graft
  • cable-ready:after-graft

Reference

inner_html

Sets the innerHTML of a DOM element.
1
inner_html(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
delay: Integer, # [0] - wait for n milliseconds before running
5
focus_selector: String, # [null] - string containing a CSS selector
6
html: String, # [null] - the HTML to assign
7
select_all: Boolean, # [false] - operate on list of elements returned from selector
8
selector: String, # required - string containing a CSS selector or XPath expression
9
xpath: Boolean # [false] - process the selector as an XPath expression
10
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-inner-html
  • cable-ready:after-inner-html

Reference

Inserts HTML into the DOM relative to an element. Positions are as follows, defaulting to beforeend:
  • beforebegin: Before the target element itself
  • afterbegin: Inside the target element, before its first child
  • beforeend: Inside the target element, after its last child
  • afterend: After the target element itself
1
insert_adjacent_html(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
delay: Integer, # [0] - wait for n milliseconds before running
5
focus_selector: String, # [null] - string containing a CSS selector
6
html: String, # [null] - the HTML to insert
7
position: String, # [beforeend] - the relative position to the DOM element (beforebegin, afterbegin, beforeend, afterend)
8
select_all: Boolean, # [false] - operate on list of elements returned from selector
9
selector: String, # required - string containing a CSS selector or XPath expression
10
xpath: Boolean # [false] - process the selector as an XPath expression
11
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-insert-adjacent-html
  • cable-ready:after-insert-adjacent-html

Reference

insert_adjacent_text

Inserts text into the DOM relative to an element. Positions are as follows, defaulting to beforeend:
  • beforebegin: Before the target element itself
  • afterbegin: Inside the target element, before its first child
  • beforeend: Inside the target element, after its last child
  • afterend: After the target element itself
1
insert_adjacent_text(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
delay: Integer, # [0] - wait for n milliseconds before running
5
focus_selector: String, # [null] - string containing a CSS selector
6
position: String, # [beforeend] - the relative position to the DOM element (beforebegin, afterbegin, beforeend, afterend)
7
select_all: Boolean, # [false] - operate on list of elements returned from selector
8
selector: String, # required - string containing a CSS selector or XPath expression
9
text: String, # [null] - the text to insert
10
xpath: Boolean # [false] - process the selector as an XPath expression
11
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-insert-adjacent-text
  • cable-ready:after-insert-adjacent-text

Reference

morph

Fast lightweight DOM diffing/patching without a virtual DOM.
1
morph(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
children_only: Boolean, # [false] - indicates if only child nodes should be morphed... skipping the parent element
5
content: String, # reference - the content that is going to be used to morph
6
delay: Integer, # [0] - wait for n milliseconds before running
7
focus_selector: String, # [null] - string containing a CSS selector
8
html: String, # [null] - the HTML to assign
9
permanent_attribute_name: String, # [null] - an attribute name that prevents elements from being updated i.e. "data-permanent"
10
select_all: Boolean, # [false] - operate on list of elements returned from selector
11
selector: String, # required - string containing a CSS selector or XPath expression
12
xpath: Boolean # [false] - process the selector as an XPath expression
13
)
Copied!
When morphing an element with children_only: true you need to make sure that the content you're providing to the html value will result in a successful operation:
  • it must have a single top-level container element with the same CSS selector as the target
  • inside that container element is another element node, not a text node
Consider the following example of #foo, a successful morphing candidate:
show.html.erb
1
<div>
2
<%= render partial: "foo", locals: {message: "Am I the medium or the massage?"} %>
3
</div>
Copied!
_foo.html.erb
1
<div id="foo">
2
<span><%= message %></span>
3
</div>
Copied!
If the <div id="foo"> was located in show.html.erb the morph would not succeed because the top-level container that you're replacing would not be present in the replacement HTML.
If you need to render a collection of partials, you'll have to wrap the render method in a container because you cannot have the top-level container in each partial:
show.html.erb
1
<div id="bar">
2
<%= render collection: @bars %>
3
</div>
Copied!
_bar.html.erb
1
<span><%= bar.message %></span>
Copied!
1
morph(
2
children_only: true,
3
selector: "#bar",
4
html: "<div id=\"bar\">" + render(Bar.all) + "</div>"
5
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-morph
  • cable-ready:after-morph
As with all operations, you can modify the items in the event.detail object inside of a before-morph to change the parameters of how the operation will be executed.
The morph operation's event.detail object has a special key called content which is a direct reference to the internal template element's content accessor holding the document fragment that will be morphed into your DOM.
This means that you can change the content which will soon be morphed into your page, but you cannot assign the content to a new value. Doing so would remove the reference!
Instead, use the DOM API and mutate children inside of the content:
1
document.addEventListener('cable-ready:before-morph', event => {
2
event.detail.content.querySelector('#foo').setStyle('color', 'red')
3
})
Copied!

Reference

outer_html

Replaces a DOM element with new HTML.
1
outer_html(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
delay: Integer, # [0] - wait for n milliseconds before running
5
focus_selector: String, # [null] - string containing a CSS selector
6
html: String, # [null] - the HTML to use as replacement
7
select_all: Boolean, # [false] - operate on list of elements returned from selector
8
selector: String, # required - string containing a CSS selector or XPath expression
9
xpath: Boolean # [false] - process the selector as an XPath expression
10
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-outer-html
  • cable-ready:after-outer-html

Reference

prepend

Inserts HTML into the DOM, inside the target element, before its first child.
1
prepend(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
delay: Integer, # [0] - wait for n milliseconds before running
5
focus_selector: String, # [null] - string containing a CSS selector
6
html: String, # [null] - the HTML to assign
7
select_all: Boolean, # [false] - operate on list of elements returned from selector
8
selector: String, # required - string containing a CSS selector or XPath expression
9
xpath: Boolean # [false] - process the selector as an XPath expression
10
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-prepend
  • cable-ready:after-prepend

Reference

remove

Removes an element from the DOM.
1
remove(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
delay: Integer, # [0] - wait for n milliseconds before running
5
focus_selector: String, # [null] - string containing a CSS selector
6
select_all: Boolean, # [false] - operate on list of elements returned from selector
7
selector: String, # required - string containing a CSS selector or XPath expression
8
xpath: Boolean # [false] - process the selector as an XPath expression
9
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-remove
  • cable-ready:after-remove

Reference

replace

Replaces a DOM element with new HTML. This operation is an alias of outer_html and has the same implementation.
1
replace(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
delay: Integer, # [0] - wait for n milliseconds before running
5
focus_selector: String, # [null] - string containing a CSS selector
6
html: String, # [null] - the HTML to use as replacement
7
select_all: Boolean, # [false] - operate on list of elements returned from selector
8
selector: String, # required - string containing a CSS selector or XPath expression
9
xpath: Boolean # [false] - process the selector as an XPath expression
10
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-replace
  • cable-ready:after-replace

Reference

text_content

Sets the text content of a DOM element.
CableReady sets textContent instead of innerText. You can learn more here, or just move on with your day. ☀️
1
text_content(
2
batch: String, # [null] - add the operation to a named batch
3
cancel: Boolean, # [false] - cancel the operation (for use on client)
4
delay: Integer, # [0] - wait for n milliseconds before running
5
focus_selector: String, # [null] - string containing a CSS selector
6
select_all: Boolean, # [false] - operate on list of elements returned from selector
7
selector: String, # required - string containing a CSS selector or XPath expression
8
text: String, # [null] - the text to assign
9
xpath: Boolean # [false] - process the selector as an XPath expression
10
)
Copied!

Life-cycle Callback Events

  • cable-ready:before-text-content
  • cable-ready:after-text-content

Reference

Last modified 4mo ago