0.4.1 • Published 2 years ago

k-util v0.4.1

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

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

APITypeDescription
data-viewHTMLComponent label. If a child of this.element has this attribute, the child and its children won't be put into this.refs
data-clickHTMLBind click listener, method will bind to this automatically.
data-onHTMLBind event listeners, multiple event-handler pairs seperated by ;. data-on="click: onClick; input: onInput;" methods will bind to this automatically
data-refHTMLDeclare DOM reference, could be refered later in this.refs
nameView Propertydefault ""
elementView PropertyRequired for front-end components
refsView Propertydefault {}
dispatchView MethodCall 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
destroyView MethodWill 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