hyperd
Version:
Virtual DOM based, template engine agnostic, a lightweight view library
249 lines (164 loc) • 5.36 kB
Markdown
Hyperd
======
[](https://travis-ci.org/nkzawa/hyperd)
Virtual DOM based, template engine agnostic, a lightweight view library.
```js
var component = hyperd(document.getElementById('count'), function() {
return '<div id="count">Counter: ' + this.data.count + '</div>';
});
component.data.count = 0;
setInterval(function() {
component.data.count++;
}, 1000);
```
$ npm install hyperd
```html
<script src="node_modules/hyperd/hyperd.js"></script>
```
$ bower install hyperd
- Virtual DOM diffing
- Template engine agnostic
- Auto-redrawing
- Building reusable components
- Small API
- [Counter](http://nkzawa.github.io/hyperd/examples/counter)
- [Counter Component](http://nkzawa.github.io/hyperd/examples/counter-component)
- [TodoMVC](http://nkzawa.github.io/hyperd-todomvc/) ([source](https://github.com/nkzawa/hyperd-todomvc))
- `node` HTMLElement Node to attach
- `render` Function Called upon redrawing, should return a html string.
- Return: hyperd.Component
A short circuit to create a simple component instance.
The base component class.
- `proto` Object protoype object
- Return: Function A new component constructor
Creates a new component class.
```js
var MyComponent = hyperd.Component.extend({
render: function() {
return '<div>hi</div>';
}
});
```
- `props` Object properties
In classes that extends `hyperd.Component`, make sure to call the super constructor so that the all settings can be initialized.
```js
hyperd.Component.extend({
constructor: function(props) {
hyperd.Component.apply(this, arguments);
...
}
});
```
The properties of the component.
```js
var component = new MyComponent({ foo: "hi" });
console.log(component.props.foo); // "hi"
```
The state data of the component. Mutating `data` triggers UI updates.
The node element which the component is attached to.
Definitions of child components. You can use defined components like a custom element on `render()`.
```js
var Child = hyperd.Component.extend({
render: function() {
return '<div>' + this.props.foo + '</div>';
}
});
hyperd.Component.extend({
components: { child: Child },
render: function() {
return '<div><child foo="hi"></div>'
}
});
```
- `node` HTMLElement
- Return: `this`
Attaches the component to a DOM node.
- Return: String A html string to render.
Note: **implement this function, but do NOT call it directly**.
Reuired to implement. This method is called automatically and asynchronously when you update `component.data`.
Teardown and delete all properties and event bindings including descendant components.
- `event` String The event type to be triggered.
- Return: `this`
Trigger a DOM event for `component.node` with the supplied arguments.
```js
component.emit('edit', arg);
```
- `event` String The event type.
- `listener` Function
- Return: `this`
Add a listener for the specified event.
```js
component.on('render', function() { ... });
```
- `event` String The event type.
- `selector` String CSS selector.
- `listener` Function The listener always take an event object as the first argument.
- Return: `this`
Add a listener for the delegated event. The listener is called only for descendants that match the `selector`. You can use this to listen an event of a child component too.
```js
hyperd.Component.extend({
constructor: function() {
hyperd.Component.apply(this, arguments);
this.on('click', 'button', function(event) {
console.log('clicked!');
});
}
render: function() {
return '<div><button type="button">Click me!</button></div>'
}
});
```
Remove a listener for the specified event.
Remove a listener for the delegated event.
Remove all listeners, or those of the specified event or the delegated event.
Called upon after the component is attached to a node.
Called upon after the component is rerendered.
Called upon after the component is destroyed.
The same as `component.onAttach`.
```js
component.on('attach', function() { ... });
```
The same as `component.onRender`.
The same as `component.onDestroy`.
The identifier used to differentiate a node. Used to reconcile an element will be reordered or destroyed.
```js
hyperd.Component.extend({
render: function() {
var items = ['foo', 'bar', 'baz'];
return '<ul>' + items.map(function(item) {
return '<li data-hkey="' + item + '">' + item + '</li>';
}) + '</ul>';
}
});
```
MIT