-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.html
224 lines (210 loc) · 8.1 KB
/
example.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
<!doctype html>
<html>
<head>
<title>Template Test</title>
<script type="text/x-platter" id="ggt">
<h1 if="{{title}}">{{title}}</h1>
<ul>
<li class="before">This is before the list</li>
<li foreach="{{blah}}" class="{{class.t}} within" onclick="{{click}}">{{name}}</li>
<li class="after">This is after the list</li>
</ul>
</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script type="text/javascript" src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script type="text/javascript" src="https://raw.github.com/Shopify/batman/master/lib/batman.js"></script>
<script type="text/javascript" src="http://cloud.github.com/downloads/SteveSanderson/knockout/knockout-2.0.0.js"></script>
<script type="text/javascript" src="http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.7.1.min.js"></script>
<script type="text/javascript" src="platter.js"></script>
<script type="text/javascript">
function goPlain(){
var template = Platter.Plain.compile(document.getElementById('ggt').text);
var data = {title: Math.random()>0.5?"This is the title":null, blah: []};
for (var i=0;Math.random()*5>i;++i)
data.blah.push({'class':{t:'row'+i}, name:'Name '+Math.random(), click:function(){alert(1);}});
var els = template.run(data).docfrag;
document.body.appendChild(els);
}
function goBackbone(){
var template = Platter.Dynamic.compile(document.getElementById('ggt').text);
var data = new Backbone.Model();
data.set({title: Math.random()>0.5?"This is the title":null});
data.blah = new Backbone.Collection();
for (var i=0;Math.random()*5>i;++i) {
var row = new Backbone.Model();
row.set({'class':{t:'row'+i}, name:'Name '+Math.random()});
row.click=function(){alert(2);};
data.blah.add(row);
}
var mungedata = function(){
data.set({title: Math.random()>0.5?"This is the title":null});
if (Math.random()*5>data.blah.length) {
var row = new Backbone.Model();
var at = Math.floor(Math.random()*(data.blah.length+1));
row.set({'class':{t:'row'+at}, name:'Name '+Math.random()});
row.click=function(){alert(2);};
data.blah.add(row, {at:at});
}
else {
var ent = data.blah.at(Math.floor(Math.random()*data.blah.length));
if (Math.random()>0.5)
data.blah.remove(ent);
else
ent.set({name:'Name '+Math.random()});
}
};
var timer = setInterval(mungedata, 1000);
var button = document.createElement('button');
var undo = new Platter.Undo
var els = template.run(data, undo).docfrag;
undo.add(function(){
document.body.removeChild(button);
clearInterval(timer);
});
document.body.appendChild(els);
button.innerHTML = "Undo";
button.onclick = function(){ undo.undo(); };
document.body.appendChild(button);
}
function goBatman(){
var template = Platter.Dynamic.compile(document.getElementById('ggt').text);
var data = new Batman.Object();
data.set('title', Math.random()>0.5?"This is the title":null);
data.blah = new Batman.Set();
for (var i=0;Math.random()*5>i;++i) {
var row = new Batman.Object();
row.set('class', {t:'row'+i});
row.set('name', 'Name '+Math.random());
row.click=function(){alert(2);};
data.blah.add(row);
}
var mungedata = function(){
data.set('title', Math.random()>0.5?"This is the title":null);
if (Math.random()*5>data.blah.length) {
var row = new Batman.Object();
// Batman can't seem to add rows in the middle...
row.set('class', {t:'row'+data.blah.length});
row.set('name', 'Name '+Math.random());
row.click=function(){alert(2);};
data.blah.add(row);
}
else {
// Batman can't seem to access an entry by index...
var ent = data.blah.toArray()[Math.floor(Math.random()*data.blah.length)];
if (Math.random()>0.5)
data.blah.remove(ent);
else
ent.set('name', 'Name '+Math.random());
}
};
var timer = setInterval(mungedata, 1000);
var button = document.createElement('button');
var undo = new Platter.Undo
var els = template.run(data, undo).docfrag;
undo.add(function(){
document.body.removeChild(button);
clearInterval(timer);
});
document.body.appendChild(els);
button.innerHTML = "Undo";
button.onclick = function(){ undo.undo(); };
document.body.appendChild(button);
}
function goKnockout(){
var template = Platter.Dynamic.compile(document.getElementById('ggt').text);
var data = {};
data.title = ko.observable(Math.random()>0.5?"This is the title":null);
data.blah = ko.observableArray();
for (var i=0;Math.random()*5>i;++i) {
var row = {};
row['class'] = ko.observable({t:'row'+i});
row.name = ko.observable('Name '+Math.random());
row.click = function(){alert(2);};
data.blah.push(row);
}
var mungedata = function(){
data.title(Math.random()>0.5?"This is the title":null);
if (Math.random()*5>data.blah().length) {
var row = {};
row['class'] = ko.observable({t:'row'+i});
row.name = ko.observable('Name '+Math.random());
row.click=function(){alert(2);};
// TODO: Add the row in a random position.
data.blah.push(row);
}
else {
var ent = data.blah()[Math.floor(Math.random()*data.blah().length)];
if (Math.random()>0.5)
data.blah.remove(ent);
else
ent.name('Name '+Math.random());
}
};
var timer = setInterval(mungedata, 1000);
var button = document.createElement('button');
var undo = new Platter.Undo
var els = template.run(data, undo).docfrag;
undo.add(function(){
document.body.removeChild(button);
clearInterval(timer);
});
document.body.appendChild(els);
button.innerHTML = "Undo";
button.onclick = function(){ undo.undo(); };
document.body.appendChild(button);
}
function goEmber(){
var template = Platter.Dynamic.compile(document.getElementById('ggt').text);
var data = Ember.Object.create();
data.set('title', Math.random()>0.5?"This is the title":null);
data.blah = Ember.Set.create();
for (var i=0;Math.random()*5>i;++i) {
var row = Ember.Object.create();
row.set('class', {t:'row'+i});
row.set('name', 'Name '+Math.random());
row.click=function(){alert(2);};
data.blah.add(row);
}
var mungedata = function(){
data.set('title', Math.random()>0.5?"This is the title":null);
if (Math.random()*5>data.blah.length) {
var row = Ember.Object.create();
// Ember can't seem to add rows in the middle...
row.set('class', {t:'row'+data.blah.length});
row.set('name', 'Name '+Math.random());
row.click=function(){alert(2);};
data.blah.add(row);
}
else {
// Ember can't seem to access an entry by index...
var ent = data.blah.toArray()[Math.floor(Math.random()*data.blah.length)];
if (Math.random()>0.5)
data.blah.remove(ent);
else
ent.set('name', 'Name '+Math.random());
}
};
var timer = setInterval(mungedata, 1000);
var button = document.createElement('button');
var undo = new Platter.Undo
var els = template.run(data, undo).docfrag;
undo.add(function(){
document.body.removeChild(button);
clearInterval(timer);
});
document.body.appendChild(els);
button.innerHTML = "Undo";
button.onclick = function(){ undo.undo(); };
document.body.appendChild(button);
}
</script>
</head>
<body>
<button onclick="goPlain()">Plain</button>
<button onclick="goBackbone()">Backbone</button>
<button onclick="goBatman()">Batman</button>
<button onclick="goKnockout()">Knockout</button>
<button onclick="goEmber()">Ember</button>
</body>
</html>