/
test.html
137 lines (129 loc) · 3.17 KB
/
test.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Fragment View Test</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.marionette/1.5.1-bundled/backbone.marionette.min.js"></script>
<script src="./FragmentView.js"></script>
<style>
#normal, .normal {
background-color: #faf;
float: left;
width: 49%;
border-bottom: 2px solid black;
}
#frag, .frag {
background-color: #ffa;
float: left;
width: 49%;
border-bottom: 2px solid black;
}
</style>
</head>
<body>
<h1>Test Page</h1>
<div>
<span class="normal">NORMAL COLLECTION VIEW</span>
<span class="frag">ADDON FRAGMENT VIEW</span>
</div>
<div id="normal"></div>
<div id="frag"></div>
<script>
$(document).ready(function() {
window.app.start();
});
/** Config **/
DATA_SIZE = 9999;
/** Data */
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomDate() {
return new Date( getRandomInt(458895600000, 1394089085507));
}
function getRandomString(len) {
var c="abcdefghijklmnopqrstuvwxyz0123456789";
var s="";
for( var i=0; i<len; i++ ) {
s += c[getRandomInt(0,c.length-1)];
}
return s;
}
function getRandomStrings() {
var max=200;
var s = "", len;
while( max > 0 ) {
len = getRandomInt(10,20);
s += getRandomString(len) + " ";
max -= len;
}
return s;
}
function buildData() {
window.data = new Backbone.Collection();
for( var i=0; i<DATA_SIZE; i++ ) {
window.data.add( new Backbone.Model({
id: _.uniqueId(),
date: getRandomDate().toUTCString(),
message: getRandomStrings()
}));
}
}
window.data = false;
/** APP */
var app = new Backbone.Marionette.Application();
var RowView = Backbone.Marionette.ItemView.extend({
template: "#tpl-row",
tagName: 'tr',
className: 'row'
});
var MyCollectionView = Backbone.Marionette.CollectionView.extend({
tagName: 'table',
itemView: RowView,
});
var MyFragmentView = Backbone.Marionette.FragmentView.extend({
tagName: 'table',
itemView: RowView
});
// module
app.module("ViewModule", function( module, app ) {
module.addInitializer(function() {
console.log("Ready");
});
function timer(msg, fn, ctx) {
var now,then;
now = new Date();
fn.call(ctx);
then = new Date();
console.log(msg, then.getTime() - now.getTime() );
}
this.exec = function() {
timer("build data", buildData, this);
timer("Fragment", doFrag, this);
timer("Normal", doNormal,this);
};
function doNormal() {
var CView = new MyCollectionView({
collection: window.data,
});
CView.render();
$("#normal").empty().append( CView.el );
}
function doFrag() {
var FView = new MyFragmentView({
collection: window.data,
});
FView.render();
$("#frag").empty().append( FView.el );
}
});
</script>
<script type="text/template" id="tpl-row">
<td><nobr><%- date %></nobr></td>
<td><%- message %></td>
</script>
</body>
</html>