-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
294 lines (270 loc) · 9.85 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Witch.js</title>
<link rel="stylesheet" href="example/components/bootstrap/dist/css/bootstrap.min.css"/>
<link rel="stylesheet" href="example/components/prism/prism.css"/>
<link rel="stylesheet" href="example/css/main.css"/>
</head>
<body>
<div>
<header>
<aside>
<a href="https://github.com/eyy/witch">
Github
<span class="brandico-github"></span>
</a>
<a href="http://empeeric.com">
Empeeric
<img src="example/img/empeeric.png" alt=""/>
</a>
</aside>
<h1>
<a href="https://github.com/eyy/witch">Witch.js</a>
<img src="example/img/icon.png" alt="" />
</h1>
<p>Against monster-frameworks and unnatural coding practices, you might need a <strong>witch</strong> at your side.</p>
</header>
<!-- Notice it is usually much better to put scripts at the end of <body>,
but because of all the inline <script>s in this page, these came up here. -->
<script src="example/components/jquery/jquery.min.js"></script>
<script src="example/components/watch/index.js"></script>
<script src="example/components/rivets/dist/rivets.js"></script>
<script src="dist/witch.js"></script>
<h2>The Basic</h2>
<p><strong>Data binding</strong> with <a href="http://rivetsjs.com">Rivets</a>, object observer with <a href="https://github.com/melanke/Watch.JS">Watch.js</a></p>
<div class="row">
<div class="col-md-3">
<h3>Output</h3>
<div class="html">
<div data-rivets="app">
<p>
<!-- One-way binding -->
My spell:
<strong data-text="data.spell"></strong>
</p>
<!-- Two-way binding -->
<input type="text" class="form-control" data-value="data.spell"/>
</div>
</div>
</div>
<script>
// The simplest javascript
var app = {
data: {
spell: 'Hello World!'
}
};
</script>
</div>
<hr>
<h2>Still Quite Basic</h2>
<p><strong>Models</strong>, collections and computed properties</p>
<div class="row">
<div class="col-md-3">
<h3>Todo</h3>
<div class="html">
<style type="text/css">
.done {
text-decoration: line-through;
color: grey;
}
</style>
<div data-rivets="todo">
<p>
<!-- Declare computed property dependencies -->
<span data-text="tasks:remaining
< .list.length .updated"></span> of
<span data-text="tasks.list.length"></span> remaining
</p>
<ul>
<!-- Loop through the collection -->
<li data-each-task="tasks.list" data-class-done="task.done">
<input type="checkbox" data-checked="task.done" />
<span data-text="task.text"></span>
</li>
</ul>
<form data-on-submit="tsk:saveAs" class="form-inline">
<input type="text" class="form-control" data-value="tsk.text"/>
<button type="submit" class="btn btn-primary">Add task</button>
</form>
</div>
</div>
</div>
<script>
// Write and inherit classes however you like
var Task = function() {
witch.Model.apply(this, arguments);
// Notifying the change
watch(this, 'done', function() {
this._collection.updated += 'a';
});
};
$.extend(Task.prototype, witch.Model.prototype);
// Our task collection
var tasks = new witch.Collection([
{ done: true, text: 'Buy a toad' },
{ done: false, text: 'Buy a bat' },
{ done: false, text: 'Fix my broom' }
], Task); // <-- is a collection of tasks
// A computed property
tasks.remaining = function() {
return this.list.filter(function(t) {
return !t.done
}).length;
};
var todo = {
tasks: tasks,
tsk: new Task({}, tasks)
};
</script>
</div>
<hr>
<h2>Basic All The Way Down</h2>
<p><strong>Templates</strong>, simple as they should be.</p>
<div class="row">
<div class="col-md-3">
<h3>Which Witch?</h3>
<div class="html">
<div data-rivets="templates">
<ul>
<li data-each-user="users.list">
<a href="#" data-text="user.name" data-on-click="user:card"></a>
</li>
</ul>
<div class="cards"></div>
</div>
<!-- A template markup. -->
<div data-template="user-card">
<div class="user-card">
<a class="close" href="#" data-on-click="tpl:close">x</a>
<strong data-text="user.name"></strong>
</div>
</div>
</div>
</div>
<script>
// Create your template
var UserCard = witch.inherit(witch.Template, {
template: 'user-card',
ready: function() {
this.el.appendTo('.cards');
},
close: function() {
this.el.remove();
}
});
var User = witch.inherit(witch.Model, {
card: function() {
// Use it
var card = new UserCard({ user: this });
card.render();
}
});
var users = new witch.Collection([
{ name: 'Morgan Le Fay' },
{ name: 'Maggie Wall' },
{ name: 'Medea' }
], User);
var templates = {
users: users
};
$(function() {
users.list[0].card();
});
</script>
</div>
<hr>
<h2>... And Basically, All The Way Up As Well</h2>
<p>
<strong>REST</strong> assure: you can fetch and save your data from/to the server. Also, a little animation example.<br />
</p>
<div class="row">
<div class="col-md-3">
<h3>REST</h3>
<div class="html">
<style type="text/css">
.flash { -webkit-animation: 2s flash; }
@-webkit-keyframes flash {
from { background: yellow; }
to { background: auto; }
}
</style>
<div data-rivets="server">
<ul>
<!-- Add a nice flash animation when adding items -->
<li data-each-item="items.list" data-class-flash="item._new">
<!-- `delete()` -->
<i class="glyphicon glyphicon-remove" data-on-click="item:delete"></i>
<span data-text="item.name"></span>
</li>
</ul>
<!-- `saveAs()` will clone the data and clean itself -->
<form data-on-submit="item:saveAs" class="form-inline">
<input type="text" class="form-control" data-value="item.name"/>
<button type="submit" class="btn btn-primary">Add Item</button>
</form>
</div>
</div>
</div>
<script>
var items = new witch.Collection([
// Fake data, pretend it's from the server
{ name: 'Spell book' },
{ name: 'Broom' },
], '/api/items/'); // <-- collection url
var server = {
items: items,
item: new witch.Model({}, items) // <-- model's collection
};
$(function() {
// Fetch 'em!
items.fetch();
});
</script>
</div>
</div>
<div>
<h4>You'll need a server. If you have git, node and mongo, you can run one:</h4>
<pre style="width:500px;"><code>git clone https://[email protected]/eyy/witch.git
cd witch
npm install
node app</code></pre>
</div>
<div data-template="code">
<div class="col-md-5">
<h3>HTML</h3>
<pre><code class="language-markup"></code></pre>
</div>
<div class="col-md-4">
<h3>Javascript</h3>
<pre><code class="language-javascript"></code></pre>
</div>
</div>
<script>
var cleanTabs = function(s) {
return s.replace(/^[ ]{12}/mg, '');
};
$('.row').each(function() {
var t = $(this),
html = cleanTabs(t.find('.html').html().trim()),
script = cleanTabs(t.find('script').text().trim());
witch.config.template('code', {}, function(err, el) {
t.prepend(el);
el.find('code.language-markup').text(html);
el.find('code.language-javascript').text(script);
});
});
</script>
<script src="example/components/prism/prism.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-26972001-2', 'witch.io');
ga('send', 'pageview');
</script>
</body>
</html>