npm install @webly/rebind
Briefly show the most common use case.
<head>
<title @text="{title}"></title>
<script type="module" defer>
import {Rebind} from '@webly/rebind';
// import {Rebind} from "https://cdn.jsdelivr.net/npm/@webly/rebind@0.3.0/dist/min/index.js"
new Rebind(document).state({
title: "hello",
say: function(arg) {
alert(this.title + arg)
}
}).run()
</script>
</head>
<body>
<h1 @text="hello world"></h1>
<button @on:click="say('world')">click</button>
</body>
Implicit variables are automatically provided. They can be accessed inside directive expressions without being explicitly defined in state.
| Variable | Description |
|---|---|
| $element | current element node |
| $select() | currentElement.querySelector |
| $selectAll() | currentElement.querySelectorAll |
@text="text content"γDescription
Sets the text content of an element. Supports string interpolation using {propertyName} to access properties from the current scope.
Accepted Values / Type
string β Plain text or text with placeholders:
Example: "Hello {fullName}"Usage Example
<p @text="Hello {fullName}"></p>
Notes / Behavior
"Hi {firstName} {lastName}"{user.profile.name}).@data='{ "name": "linus"}'γDescription
Binds JSON data to the element. Supports direct JSON objects or dynamic properties from the scope, including nested properties.
Accepted Values / Type
<div @data='{ "name": "linus" }'></div>
<div @data="user"></div>
<div @data="user.profile"></div>
Usage Example
<div @data='{ "name": "linus" }'>
<p @text="Hello {name}"></p>
</div>
Notes / Behavior
@skip=""γDescription
Skips the element and all its children. None of its directives or content will run.
Usage Example
<div @skip>
<p @text="skiped"></p>
</div>
<p @text="not skiped"></p>
@html="html"γDescription
Set innerHTML to the element
Usage Example
<div @html="<p>Hello World</p>"></div>
<div @html="{html}"></div>
@:attributeName="value"γDescription
Sets or creates an HTML attribute on the element. Supports string interpolation using {property} or nested {object.property} from the current scope. Property names cannot contain spaces.
Usage Example
<button @:class="btn {color}"></button>
@on:eventType="functionName(arg)"γDescription
Add event listener to the element.
Usage Example
<button @on:click="increment()" @text="{count}">0</button>
<script>
const rootData = observe({
count: 0,
increment() {
console.log(this.$event)
this.$event.preventDefault()
this.count += 1
}
})
new Rebind(document.body.state(rootData).run())
</script>
@init="functionName(arg)"γDescription
Runs a function from the scope after @data has been initialized on the element.
Accepted Values / Type
<div @init="setup(user)"></div>
@for=""γDescription
Iterates over an array or iterable and renders the contents of the <template> for each element.
During iteration, the directive implicitly creates local variables that are available inside the template scope.
The <template> element itself is not rendered; its content is cloned and inserted into the DOM for each item in the collection.
Implicit Variables
The directive automatically exposes the following variable inside the template:
| variable | Description |
|---|---|
| $key | index or key of the iteration |
Usage Example
<ul @data="data">
<template @for="animal in animals">
<li @text="{animal.name}">item</li>
</template>
</ul>
<script>
new Rebind(document.body)
.state({
data: {
animals: [
{ name: "cat", tail: true },
{ name: "husky", tail: true }
]
}
})
.run()
</script>
@if="Foo ConditionalOperator Bar; functionName()"γDescription
Run function when condition is true.
Usage Example
<div @data='{"count": 10}'>
<div @if="{count} > 5"; show()">
<p hidden>Hello World</p>
</div>
</div>
<script>
new Rebind(document.body).state({
show() {
const p = this.$element.querySelector("p")
if (p) p.hidden = !p.hidden
}
}).run()
</script>
Distributed under the MIT License. See LICENSE for more information.