The Cubbles mutation based start event API
Last updated
Last updated
Cubbles allows users to observe the mutations of an HTML node (See ) and then dispatch an event if a mutation occurs. The mutations that can be observed are the ones corresponding to {childList: true}
.
The contains a utility called mutation-based-cubx-startevent
which contains the following attributes:
data-cubx-target-selector: to provide a css selector to indicate which node should be observed
data-cubx-emit-event: to indicate the name of the event, which should be dispatched when a mutation occurs.
It is important to highlight that the only mutation to be observed is the first one that occurs; after that, the event will be dispatched and the observer will be disconnected.
The following sections present a demo to show the proper use of the mutation-based-cubx-startevent
utility.
We will use the component in our example. Thus, it should be available within the store you are currently using
Let's say say you are building an app where a Cubbles component will be included. The component will be appended to a div container, but you need the Cubbles RTE to start working after this div suffers a mutation. Thus, you need to use the mutation-based-cubx-startevent.
To use the mutation-based-cubx-startevent
first we need to:
Include it as script within the head of our HTML document
Add a root dependency to include the cubx-textarea
in our page
Include the custom-elements-es5-adapter
, webcomponents-lite
and the crc-loader
scripts as usual.
Provide a value for the data-cubx-startevent
within the crc-loader
script since we want the Cubbles RTE to start working after a mutation occurs
Lets say we want to observe a div, whose id is observable. Also, we want an event called mutationBasedStart to be dispatched when the mutation occurs. Therefore, the values of the mutation-based-cubx-startevent
attributes should be:
Attribute
Value
data-cubx-target-selector
#observable
data-cubx-emit-event
mutationBasedStart
Using the information presented above, the head element of our app should look similar to the one shown below:
As you can already imagine, the body of our app should have a div container with an id equals to observable. That will be enough, however we want our app to explain how our mutation-based-cubx-startevent
works, so we will also include the following elements:
A title for our app
A description of the values of the attributes
A description explaining how the mutation-based-cubx-startevent
works in this particular case
A button to append the component called cubx-textarea
And a loader to be shown while the Cubbles RTE is working
The code of the body of our app should now look as follows:
We need a script to:
Append the cubx-textarea
component to the observable div after the appendComp button is clicked.
Show the loader while the Cubbles RTE is working
Hide the loader and show the observable when the component is ready
Our code should look similar to the one shown below:
Now we need to style the loader div, so that it looks like a spinner and let the user know that the component is loading; i.e., the Cubbles RTE is working on getting the component ready. Additionally, we want the observable div to be hidden by default. The style should look as follows:
The result of the code above should look as follows:
Sometimes you would like to add the target node for the mutation observer dynamically; in that case, the utility mutation-based-cubx-startevent
will wait until the node is added to the body to observe it for changes.
The only thing should do is to append a target node that matches the css selector provided in the data-cubx-target-selector
attribute.
Create and init the cubx-textarea
component using the .
Check to see the result working online.
Check to see the result working online.