Template
MMO.core · 1 symbol · generated 5 Oct 2026
Template
Template.js:7Base class for View
- Namespace
MMO.core
Index
| promise readonly accessor | Resolved once every asynchronous piece of the template (custom elements, sub views) has finished rendering. | |
| lastChild readonly accessor | The last node the template rendered at its root level. | |
| firstChild readonly accessor | The first node the template rendered at its root level. | |
| parentNode readonly accessor | ||
| nextSibling readonly accessor | Get next sibbling of last node. | |
| constructor(node, ref) | Constructor | |
| Define(name, fn) static | Register a renderer under a name, so it can be rendered by name later. | |
| IsDefined(name) static | ||
| RenderTemplate(name, node, args) static | Render a registered renderer into a node. | |
| querySelector(selector) | Find the first rendered node matching a selector. | |
| querySelectorAll(selector) | Mimic querySelectorAll but return a Set instead of a NodeList | |
| setRoot(node, ref) | Point the template at the node it renders into. | |
| updateNamespace() | Remember the xml namespace of the node currently open, so createNode keeps building in it: inside an <svg>, open('rect') must produce an svg rect, not an html one. | |
| createNode(tag) | Build an element in the template's current namespace. | |
| add(node, attrs, children) | Insert a node at the template's current position, then move the cursor past it. | |
| detach() | Detach view from dom. | |
| reattach() | Reattach view into dom | |
| remove() | Remove view from dom | |
| hide() | Hide the rendered nodes, remembering the display each one had. | |
| show() | Undo MMO.core.Template#hide, restoring the display each node had. | |
| insertBefore(parent, ref) | Move every rendered node into another parent, and re-root the template there. | |
| insertAfter(node) | Move every rendered node just after another node. | |
| setRenderer(renderer) | Attach the function that draws this template. | |
| hasRenderer() | ||
| render(args) | Run the renderer, with the template as scope. | |
| setScope($this) | Set $this variable inside an attribute handle (@example onclick="console.log(this, $this)") if function scope has not been set this == $this. | |
| getScope() | The object $this refers to inside attribute handlers. | |
| addEvent(node, name, fn) | Attach a dom listener AND remember it, so MMO.core.Template#remove can unbind it. | |
| setAttribute(node, name, value) | Set attribute of the node. | |
| attr(name, value) | Set an attribute on the node currently open. | |
| text(value) | Insert a text node at the template's current position. | |
| html(html) | Parse and insert html at the template's current position. | |
| open(tag, attrs) | Insert a node and descend into it: everything added next becomes its child, until | |
| close(tag) | Close the node currently open and go back to its parent. | |
| closeAll() | Close every open node and go back to the root. |
Properties5
promise
readonlyaccessorTemplate.js:27Resolved once every asynchronous piece of the template (custom elements, sub views) has finished rendering.
- Type
Promise<this>
lastChild
readonlyaccessorTemplate.js:107The last node the template rendered at its root level. A detached node reports its placeholder, so the position in the dom stays usable.
- Type
?Node
firstChild
readonlyaccessorTemplate.js:119The first node the template rendered at its root level.
- Type
?Node
parentNode
readonlyaccessorTemplate.js:129- Type
Element
nextSibling
readonlyaccessorTemplate.js:138Get next sibbling of last node.
- Type
[type]
Methods30
constructor(node, ref)
Template.js:14Constructor
| node = null | Element | the dom element to insert nodes in |
| ref = null | Element | null | null: push nodes in node. Element: insert nodes after this one |
Define(name, fn)
staticTemplate.js:38Register a renderer under a name, so it can be rendered by name later.
| name | string | |
| fn | Function | the renderer; it is called with the template as scope |
| throws | MMO.Error | when the name is already taken |
IsDefined(name)
staticTemplate.js:49| name | string | |
| returns | boolean | true when a renderer goes by that name |
RenderTemplate(name, node, args)
staticTemplate.js:61Render a registered renderer into a node.
| name | string | the registered renderer's name |
| node | Element | the dom node to render into |
| ...args | ...* | arguments for the renderer |
| returns | MMO.core.Template | |
| throws | MMO.Error | when no renderer goes by that name |
querySelector(selector)
Template.js:76Find the first rendered node matching a selector. Unlike the dom method, the template's own top level nodes are candidates too, not only their descendants.
| selector | string | |
| returns | ?Element |
querySelectorAll(selector)
Template.js:88Mimic querySelectorAll but return a Set instead of a NodeList
| selector | string | a valid css selector |
| returns | Set |
setRoot(node, ref)
Template.js:148Point the template at the node it renders into.
| node | Element | |
| ref = null | ?Element | insert after this child of node instead of at the start |
updateNamespace()
Template.js:158Remember the xml namespace of the node currently open, so createNode keeps building in it: inside an <svg>, open('rect') must produce an svg rect, not an html one.
createNode(tag)
Template.js:168Build an element in the template's current namespace.
| tag | string | |
| returns | Element |
add(node, attrs, children)
Template.js:185Insert a node at the template's current position, then move the cursor past it.
| node | string | Node | a tag name, or an existing node |
| attrs = null | ?(Object | Function | Array) | MMO.core.dom.setAttributes |
| ...children | ...* | MMO.core.dom.appendChildren: a string is text, markup goes through MMO.core.dom.html() |
| returns | this |
detach()
Template.js:208Detach view from dom. you can reattach the view using attach
reattach()
Template.js:221Reattach view into dom
remove()
Template.js:232Remove view from dom
hide()
Template.js:251Hide the rendered nodes, remembering the display each one had.
show()
Template.js:272Undo MMO.core.Template#hide, restoring the display each node had.
insertBefore(parent, ref)
Template.js:292Move every rendered node into another parent, and re-root the template there.
| parent | Element | the new parent |
| ref = null | ?Node | insert before this node; null appends |
insertAfter(node)
Template.js:311Move every rendered node just after another node. MMO.core.Template#insertBefore
| node | Node |
setRenderer(renderer)
Template.js:322Attach the function that draws this template. Pass null to drop it.
| renderer | ?Function | called with the template as scope |
hasRenderer()
Template.js:330| returns | boolean | true when a renderer is attached |
render(args)
Template.js:340Run the renderer, with the template as scope.
| ...args | ...* | arguments for the renderer |
| returns | Promise<this> | resolved once the asynchronous parts have rendered too |
| throws | MMO.Error | when no renderer is attached |
setScope($this)
Template.js:352Set $this variable inside an attribute handle (@example onclick="console.log(this, $this)") if function scope has not been set this == $this.
| $this | Object | null |
getScope()
Template.js:361The object $this refers to inside attribute handlers.
| returns | Object | the scope set by setScope, or the template itself |
addEvent(node, name, fn)
Template.js:376Attach a dom listener AND remember it, so MMO.core.Template#remove can unbind it.
| node | Node | |
| name | string | the event name, without the on prefix |
| fn | Function | string | a handler, or source compiled as function(event, $this, $view) with this bound to the node |
setAttribute(node, name, value)
Template.js:401Set attribute of the node.
| node | Element | the node to set the attribute for |
| name | string | the attribute/property/event to set |
| value | * | any value (does not have to be a string) |
attr(name, value)
Template.js:420Set an attribute on the node currently open.
| name | string | MMO.core.Template#setAttribute for the on* and set-* forms |
| value | * | |
| returns | this |
text(value)
Template.js:438Insert a text node at the template's current position.
| value | string | |
| returns | this |
html(html)
Template.js:453Parse and insert html at the template's current position. It is what a .jhs file's static text compiles to, and all: markup its author wrote. Never hand it data, it runs (MMO.core.Template#text).
| html | string | |
| returns | this |
open(tag, attrs)
Template.js:476Insert a node and descend into it: everything added next becomes its child, until
- See
MMO.core.Template#close
| tag | string | Element | a tag name, or an existing element |
| attrs = null | ?(Object | Function | Array) | |
| returns | this |
close(tag)
Template.js:491Close the node currently open and go back to its parent.
| tag = null | ?string | close everything up to the closest node with this tag name |
| returns | this |
closeAll()
Template.js:510Close every open node and go back to the root.
| returns | this | |
| throws | MMO.Error | when the open node is not under the root any more |