-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
155 lines (154 loc) · 5.83 KB
/
index.html
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!doctype html>
<html>
<head>
<title>Jexia - riot - todo</title>
<link rel="stylesheet" href="todo.css">
<link rel="stylesheet" href="minimal.css">
</head>
<body>
<todo></todo>
<jexia></jexia>
<script type="riot/tag">
<todo>
<div class="row">
<div class="col-12"><h3>{opts.title}</h3></div>
</div>
<div class="row">
<div class="col-12"><hr/></div>
</div>
<div class="row" each={items}>
<div class="col-1">
<label class={ completed: done }>
<input type="checkbox" checked={done} onclick={toggle}>
</label>
</div>
<div class="col-10">{title}</div>
<div class="col-1" align="right">
<button onclick="{remove}">X</button>
</div>
</div>
<form onsubmit={create}>
<div class="row">
<div class="col-6">
<input name="input" onkeyup={edit}>
</div>
<div class="col-6" align="right">
<button disabled={!text}>+</button>
</div>
</div>
</form>
this.items = opts.items;
remove(e) {
this.trigger('remove', e.item);
}
edit(e) {
this.text = e.target.value;
}
create(e) {
if (this.text) {
this.trigger('create', {title: this.text});
this.text = this.input.value = '';
}
return false;
}
toggle(e) {
var item = e.item;
item.done = !item.done;
this.trigger('modify', item);
return true;
}
</todo>
</script>
<script type="riot/tag">
<jexia>
this.url = opts.url;
this.token = opts.token;
get() {
this.ajax(this.url, 'get', 'got');
}
create(data) {
this.ajax(this.url, 'post', 'created', data);
}
modify(id, data) {
this.ajax(this.url + id, 'put', 'modified', data);
}
remove(id) {
this.ajax(this.url + id, 'delete', 'removed');
}
ajax(url, method, trigger, data) {
var self = this;
$.ajax({
url: url,
type: method,
data: data,
headers: {
'Authorization': 'Bearer ' + this.token
}
}).
done(function(data) {
$.each(data, function(index, item) {
if(item && item.done) {
item.done = (item.done === "true");
}
});
self.trigger(trigger, data);
});
}
</jexia>
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/riot/2.3.1/riot+compiler.min.js"></script>
<script type="text/javascript">
function boot(url, token) {
riot.compile(function() {
todo = riot.mount('todo', {
title: 'What do you jexia today?',
items: []
})[0];
todo.on('create', function(data) {
jexia.create(data);
});
todo.on('modify', function(data) {
jexia.modify(data.id, data);
});
todo.on('remove', function(data) {
jexia.remove(data.id);
});
jexia = riot.mount('jexia', {url: url, token: token})[0];
jexia.on('got', function(data) {
todo.items = data;
todo.update();
});
jexia.on('created', function(data) {
jexia.get();
});
jexia.on('removed', function(data) {
jexia.get();
});
jexia.on('modified', function(data) {
jexia.get();
});
jexia.get();
});
}
var appId = 'JEXIA_APP_ID', // TODO: replace with your Jexia AppId
dataApp = 'http://' + appId + '.app.jexia.com/',
dataSet = dataApp + 'todo/',
todo,
jexia;
$(document).ready(function() {
$.ajax({
url: dataApp,
type: 'post',
data: {
key: 'JEXIA_KEY', // TODO: replace with the Jexia key
secret: 'JEXIA_SECRET' // TODO: replace with the secret to access the key
}
}).
done(function(data) {
boot(dataSet, data.token);
});
});
</script>
</body>
</html>