This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
browser.js
74 lines (64 loc) · 1.88 KB
/
browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
var document = require('global/document');
var mercury = require('../../../index.js');
var h = mercury.h;
var Immutable = require('immutable');
var Cursor = require('immutable/contrib/cursor');
var events = mercury.input(['add']);
var data = Immutable.fromJS({
description: 'app state description',
items: [
{ name: 'one', value: 'first', color: 'red' },
{ name: 'two', value: 'second', color: 'blue' }
],
events: events
});
var appState = Cursor.from(data);
events.add(function add(newItem) {
appState.update('items', function pushItem(items) {
return items.push(Immutable.fromJS(newItem));
});
});
function render(state) {
return h('div', [
h('h2', state.get('description')),
h('ul', state.get('items').map(function toitem(item) {
return h('li', {
key: item.cid,
style: { color: item.get('color') }
}, [
h('span', item.get('name')),
h('input', { value: item.get('value') })
]);
}).toArray()),
h('div', {
'ev-event': mercury.submitEvent(state.getIn(['events', 'add']))
}, [
h('input', {
name: 'name',
placeholder: 'name'
}),
h('input', {
name: 'value',
placeholder: 'value'
}),
h('input', {
name: 'color',
placeholder: 'color'
}),
h('button', 'add')
])
]);
}
function toObserv(cursor) {
return function observ(listener) {
if (!listener) {
return cursor.deref();
}
cursor._onChange = function onChange(newData) {
cursor._rootData = newData;
listener(newData);
};
};
}
mercury.app(document.body, toObserv(appState), render);