0.4.1 • Published 2 years ago
k-util v0.4.1
k-util
A Helper Class for Building Communicative Components.
1.5KB Gzipped, no dependencies, compatible with all modern browsers.
Installation
npm i k-util
Get Started
<div id="counter">
<p data-ref="p"></p>
<button data-click="onBtnClick">Click Me</button>
</div>
import { View } from "k-util";
class Counter extends View {
name = "counter";
clicked = 0;
onBtnClick() {
this.refs.p.textContent = ++this.clicked;
}
}
const counter = new Counter(document.getElementById("counter"));
const controller = new View();
// Components' initialization is asynchronous.
setTimeout(() => {
counter.refs.btn.click();
console.log(counter.refs.p.textContent); // 1
// Call method from an external component.
controller.dispatch("counter.onBtnClick");
console.log(counter.refs.p.textContent); // 2
counter.destroy(); // Will remove the counter element from DOM tree
v.destroy();
});
Documentation
API | Type | Description |
---|---|---|
data-view | HTML | Component label. If a child of this.element has this attribute, the child and its children won't be put into this.refs |
data-click | HTML | Bind click listener, method will bind to this automatically. |
data-on | HTML | Bind event listeners, multiple event-handler pairs seperated by ; . data-on="click: onClick; input: onInput;" methods will bind to this automatically |
data-ref | HTML | Declare DOM reference, could be refered later in this.refs |
name | View Property | default "" |
element | View Property | Required for front-end components |
refs | View Property | default {} |
dispatch | View Method | Call other components' method. this.dispatch("anotherComponent.someMethod", 1, 2, 3) will call the method someMethod of the component with name "anotherComponent", 1, 2, 3 will be the parameters |
destroy | View Method | Will remove this.element from the DOM tree, and stop listening calls from other components |
0.4.1
2 years ago
0.4.0
3 years ago
0.3.17
3 years ago
0.3.19
3 years ago
0.3.18
3 years ago
0.3.16
3 years ago
0.3.15
3 years ago
0.3.14
3 years ago
0.3.13
3 years ago
0.3.12
3 years ago
0.3.11
3 years ago
0.3.10
3 years ago
0.3.9
3 years ago
0.3.8
3 years ago
0.3.6
3 years ago
0.3.5
3 years ago
0.3.7
3 years ago
0.3.4
3 years ago
0.3.3
3 years ago
0.3.2
3 years ago
0.3.1
3 years ago
0.3.0
3 years ago
0.2.3
5 years ago
0.2.2
5 years ago
0.2.1
6 years ago
0.2.0
6 years ago
0.1.3
7 years ago
0.1.2
7 years ago
0.1.1
7 years ago
0.1.0
7 years ago