Skip to content
This repository was archived by the owner on Jun 16, 2020. It is now read-only.

Commit bf527fb

Browse files
committed
Add example real-dom
1 parent 9a4a9a7 commit bf527fb

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ hg.app(document.body, App(), App.render);
5858
- [bmi-counter](examples/bmi-counter.js)
5959
- [canvas](examples/canvas.js)
6060
- [async-state](examples/async-state.js)
61+
- [real-dom](examples/real-dom.js)
6162

6263
### Intermediate Examples
6364

bin/example-tasks.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var examplesTasks = [
2929
browserifyEditorTask('shared-state'),
3030
browserifyEditorTask('count'),
3131
browserifyEditorTask('canvas'),
32-
browserifyEditorTask('async-state')
32+
browserifyEditorTask('async-state'),
33+
browserifyEditorTask('real-dom')
3334
];
3435

3536
module.exports = examplesTasks;

examples/async-state.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function App() {
1717
}
1818

1919
App.render = function render(state) {
20-
return h('div.counter', [
20+
return h('div', [
2121
'The state has been updated asynchronously: ' + state.isUpdated
2222
]);
2323
};

examples/real-dom.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
var document = require('global/document');
4+
var hg = require('../index.js');
5+
var h = require('../index.js').h;
6+
var setTimeout = require('timers').setTimeout;
7+
8+
function Hook() {}
9+
// Hook to be called when DOM node has been rendered
10+
Hook.prototype.hook = function hook(node) {
11+
node.innerText = 'This is set directly on the real DOM node.';
12+
};
13+
// Hook to be called when DOM node is removed
14+
Hook.prototype.unhook = function unhook(node) {
15+
node.innerText = 'Unhook hook called';
16+
};
17+
18+
function App() {
19+
var state = hg.state({
20+
remove: hg.value(false)
21+
});
22+
setTimeout(function timer() {
23+
state.remove.set(true);
24+
}, 2000);
25+
return state;
26+
}
27+
28+
App.render = function App(state) {
29+
return !state.remove ?
30+
// Create virtual node with hook
31+
h('div', {
32+
hook: new Hook()
33+
}) :
34+
// Node without hook
35+
h('div');
36+
};
37+
38+
hg.app(document.body, App(), App.render);

0 commit comments

Comments
 (0)