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 142
/
browser.js
80 lines (69 loc) · 1.79 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
75
76
77
78
79
80
'use strict';
var document = require('global/document');
var window = require('global/window');
var mercury = require('../../../index.js');
var backbone = require('backbone');
var h = mercury.h;
var toObserv = require('./observ-backbone.js');
var Item = backbone.Model.extend({
defaults: {
name: '',
value: '',
color: ''
}
});
var Items = backbone.Collection.extend({
model: Item
});
var AppState = backbone.Model.extend({
defaults: {
description: '',
events: {},
items: []
}
});
var events = mercury.input(['add']);
var appState = new AppState({
description: 'app state description',
events: events,
items: new Items([
{ name: 'one', value: 'first', color: 'red' },
{ name: 'two', value: 'second', color: 'blue' }
])
});
events.add(function add(data) {
appState.get('items').add(data);
});
window.state = appState;
mercury.app(document.body, toObserv(appState), render);
function render(state) {
return h('div', [
h('h2', state.description),
h('ul', state.items.map(function toItem(item) {
return h('li', {
key: item.cid,
style: { color: item.color }
}, [
h('span', item.name),
h('input', { value: item.value })
]);
})),
h('div', {
'ev-event': mercury.submitEvent(state.events.add)
}, [
h('input', {
name: 'name',
placeholder: 'name'
}),
h('input', {
name: 'value',
placeholder: 'value'
}),
h('input', {
name: 'color',
placeholder: 'color'
}),
h('button', 'add')
])
]);
}