# model/ui

Source: https://zotonic.com/cotonic/models/ui

URI: https://zotonic.com/id/2737

Language: en



<a name="model.ui"></a>
The ui model can be used to update the dom by publishing messages on one of the <tt>model/ui/#</tt> topics. Elements can be inserted, deleted and updated. If elements in the dom-tree are updated, then a message <tt>model/ui/event/dom-updated/+key</tt> is published. This can be used to react to dom-changes when they happen.
<a name="model.ui.insert.key"></a>
insert/+key
-----------

Insert a new ui snippet named <tt>key</tt> into the user interface composer. Expects a message with the following properties.

<dl>
                <dt><tt>initialData</tt></dt>
                <dd>The initial data to place in the dom tree.</dd>

                <dt><tt>inner</tt></dt>
                <dd>If set to <tt>true</tt>, the ui composer will update the inner html, when set
                to <tt>false</tt>, the outer html</dd>

                <dt><tt>priority</tt></dt>
                <dd>An element with a high priority will be rendered before elements with a lower
                priority.
                <!-- needs example -->
                </dd>
            </dl>

```
self.publish("model/ui/insert/root", {
    initialData: "<div class='loading'>Loading</div>",
    inner: true
    priority: 100
}, {retain: true});
=> When the ui composer receives this message it will add the
   loading
            
```
<a name="model.ui.update.key"></a>
update/+key
-----------

Update the contents of ui snippet named <tt>key</tt> previously registered by an <tt>insert</tt>. Expects a message with the html snippet as message.

```
self.publish("model/ui/update/root",
    "<div class='main-app'>" +
        "..." +
    "</div>");
=> The root element is updated.
```
<a name="model.ui.delete.key"></a>
delete/+key
-----------

Delete ui snippet named <tt>key</tt> from the user interface composer.

```
self.publish("model/ui/delete/root");
=> The root element is removed from the composer, but
   stays in the DOM tree.
```
<a name="model.ui.render"></a>
render
------

Trigger a render of all registered elements.

```
self.publish("model/ui/render");
```
<a name="model.ui.render.key"></a>
render/+key
-----------

Trigger a render of the element <tt>key</tt>

```
self.publish("model/ui/render.root");
```
<a name="model.ui.render-template.key"></a>
render-template/+key
--------------------

Request rendering a template with parameters. A rendering engine must be available to respond to the render requests. The render result will update <tt>key</tt> element. Note: The element must first be inserted with [<tt>cotonic.ui.insert</tt>](/id/doc_cotonic_cotonic_ui#cotonic.ui.insert) or [<tt>model/ui/insert/+key</tt>](/id/doc_cotonic_model_ui#model.ui.insert.key). The payload of the render request should be:

<dl>
                <dt><tt>topic</tt></dt>
                <dd>The topic on which the render engine is subscribed. Can be a local or a remote topic.
                The response of the renderer will be updated on <tt>key</tt></dd>

                <dt><tt>data</tt></dt>
                <dd>The data which the render engine can use to render the template.</dd>

                <dt><tt>dedup</tt></dt>
                <dd>Send the render request via a MQTT QoS 1 message. This means that we can
                get multiple responses. Dedup makes sure the result will be rendered only once.</dd>
            </dl>

```
self.publish("model/ui/render-template/root",
             {topic: "bridge/origin/model/template/get/render/hello.tpl",
              dedup: true,
              data: {name: "Bob"}
             });
==> The render engine listening at bridge/origin/model/template receives
    a render request. The render result will be placed in element root.
```
<a name="model.ui.replace.key"></a>
replace/+key
------------

Replace an element with id <tt>key</tt> with a new element or elements. If the new HTML is passed as a string then the new DOM tree can have multiple elements.  
The replacement is done once and no further updates are registered.

```
self.publish("model/ui/replace/eltid", "Hello <b>and here we are</b>");
            
```
<a name="model.ui.event.recent-activity"></a>
event/recent-activity
---------------------

A message containing the user's activity status is published regularly on this topic. The message is an object which contains a <tt>is_active</tt> boolean property which indicates if the has been active over the last period between publishes.

```
// worker example
self.subscribe("model/ui/event/recent-activity",
    function(m) {
        const a = m.payload.is_active?"active":"not active";
        console.log("The user is: ", a);
    }
);
=> Displays the user's activity status.
```
<a name="model.ui.event.dom-updated"></a>
event/dom-updated/+key
----------------------

When the dom-tree is updated a message will be published. The message is fired after the dom-tree is updated.

```
cotonic.broker.subscribe("model/ui/event/dom-updated/message-container",
    function(m) {
        const c = document.getElementById("message-container");
        c.scrollTop = c.scrollHeight
    }
);
=> Scrolls the messages bubble container to the bottom.
            
```
<!-- ui status --> <a name="model.ui.event.ui-status"></a>
event/+model/ui-status
----------------------

Events send to this topic will result in class and data attributes set on the <tt>html</tt> of the document. The <tt>model</tt> attribute is used in the data and class attributes. This can be used to signal the state of a model to the document. This can be used to signal the state of cotonic workers or models to other ui libraries like [Elm](https://elm-lang.org) or [React](https://reactjs.org). The payload of the message:

<dl>
                <dt><tt>status</tt></dt>
                <dd>Object with model statuses. Will be set as data attributes.</dd>
                <dt><tt>classes</tt></dt>
                <dd>List with model statuses. Will be set as classes.</dd>
            </dl>

```
self.publish("model/auth/event/ui-status",  {status: {"auth": "anonymous"},
                                             classes: ["anonymous"]});
==> model.ui adds clas "ui-state-auth-anonymous" and
    adds data-ui-state-auth-auth="anonymous" to the html element of the document.
```
<!-- event/new-shadow-root/+key --> <a name="model.ui.event.new-shadow-root.key"></a>
event/new-shadow-root/+key
--------------------------

Init the topic event listener when new shadow roots are added.

<dl>
                <dt><tt>id</tt></dt>
                <dd>Element id</dd>
            </dl>

```
cotonic.broker.subscribe("model/ui/event/new-shadow-root/data-list",
    function(m) {
         console.log("Init the event listener for", "data-list")
    }
);
=> Init the topic event listener when new shadow roots are added.
```
<!-- lifecycle model -->

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2737?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/2737?language=en>)
