forked from azproduction/rivets-backbone-adapter
/
index.html
220 lines (193 loc) · 6.35 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
<!DOCTYPE html>
<html>
<head>
<title>tinybind Backbone Adapter example</title>
<script type="text/javascript" src="vendors/jquery.min.js"></script>
<script type="text/javascript" src="vendors/underscore.min.js"></script>
<script type="text/javascript" src="vendors/backbone.min.js"></script>
<script type="text/javascript" src="vendors/backbone-relational.js"></script>
<script type="text/javascript" src="vendors/tinybind.js"></script>
<script type="text/javascript" src="../tinybind-backbone.js"></script>
<style type="text/css">
.body,
.log {
float: left;
margin-right: 15px;
}
</style>
</head>
<body>
<div class="android">
<h1>Android "<span rv-text="android:name"></span>"</h1>
<div class="body">
<div>Name: <input type="text" rv-value="android:name"/></div>
<h3>Options</h3>
<ul>
<!-- 1) Nested models support -->
<li>Model <input type="text" rv-value="android:options:model"></li>
<li>Year <input type="text" rv-value="android:options:year"></li>
<li>Release <input type="text" rv-value="android:options:release"></li>
</ul>
<!-- 5) Star keypath returns all model's attributes and listens to change event -->
<div>Summary: <span rv-text="android:options.format | compute android:options:*"></span></div>
<div><textarea cols="30" rows="6" rv-value="android:options:* | json"></textarea></div>
<h2>Jobs <button rv-on-click="android.addJob">Add</button></h2>
<ul>
<!-- 2) Nested collections support -->
<li rv-each-job="android:jobs" rv-id="job.cid">
<p>
Job name
<input type="text" rv-value="job:name"/>
<button rv-on-click="job.destroy">Remove</button>
</p>
<h3>
Offices
<button rv-on-click="job.addOffice">Add</button>
</h3>
<ul>
<!-- 3) Deep nested collections support -->
<li rv-each-office="job:offices" rv-id="office.cid">
Office name
<input type="text" rv-value="office:name"/>
<button rv-on-click="office.destroy">Remove</button>
</li>
</ul>
</li>
</ul>
<h3>Bind to a collection using default adapter</h3>
<ul>
Items <button rv-on-click="addItem">Add</button> <button rv-on-click="removeItem">Remove</button>
<li rv-each-item="items:">
{item:name}
</li>
</ul>
</div>
<pre class="log"></pre>
</div>
<script type="text/javascript">
//
// tinybind Formatters for *-keypath example
//
tinybind.formatters.json = {
read: function(object) {
return JSON.stringify(object, null, 4);
},
publish: function(string) {
return JSON.parse(string);
}
};
tinybind.formatters.compute = function(value) {
return value
}
//
// Backbone Models
//
var Office = Backbone.RelationalModel.extend({
initialize: function () {
var name = 'Office id: ' + Math.random().toString(16).slice(2, 10);
this.set('name', name);
_.bindAll(this, 'destroy');
},
destroy: function () {
// do not return any value to prevent Prevent Default
Backbone.RelationalModel.prototype.destroy.call(this);
}
});
var Job = Backbone.RelationalModel.extend({
defaults: {
name: 'Coding'
},
relations: [{
type: Backbone.HasMany,
key: 'offices',
relatedModel: Office
}],
initialize: function () {
var name = ['Coding JavaScript', 'Coding HTML', 'Coding Perl', 'Coding Python'][ 0 | Math.random() * 4];
this.set('name', name);
_.bindAll(this, 'addOffice', 'destroy');
},
destroy: function () {
// do not return any value to prevent Prevent Default
Backbone.RelationalModel.prototype.destroy.call(this);
},
addOffice: function () {
this.get('offices').add({});
}
});
var Options = Backbone.RelationalModel.extend({
defaults: {
model: '42',
year: new Date().getFullYear(),
release: Math.random().toString(16).slice(2, 10)
},
format: function () {
return _.map(this.attributes, function(v, k) { return k + ': ' + v; }).join(', ');
}
});
var Android = Backbone.RelationalModel.extend({
defaults: {
name: 'Android',
options: {}
},
relations: [{
type: Backbone.HasMany,
key: 'jobs',
relatedModel: Job
}, {
type: Backbone.HasOne,
key: 'options',
relatedModel: Options
}],
initialize: function () {
this.set('name', 'Android id ' + Math.random().toString(16).slice(2, 10));
_.bindAll(this, 'addJob');
},
addJob: function () {
this.get('jobs').add({});
}
});
var items = new Backbone.Collection([
{name: 'item 1'},
{name: 'item 2'},
{name: 'item 3'}
])
//
// Backbone Views
//
var AndroidView = Backbone.View.extend({
initialize: function () {
// rv-bind part
this.binding = tinybind.bind(this.el, {
android: this.model,
items: items,
addItem() {
items.add({name: 'New Item'})
},
removeItem() {
items.pop()
}
});
},
jsonData: function () {
return JSON.stringify(this.model.toJSON(), null, 4);
}
});
// Create android
var android = new AndroidView({
el: $('.android')[0],
// Data skeleton
model: new Android({
jobs: [{
offices: [{
}]
}]
})
});
// inspect data
$(document).click(function () {
$('pre').text(android.jsonData());
}).click();
</script>
</body>
</html>