-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample03.html
103 lines (98 loc) · 3.18 KB
/
sample03.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
<!DOCTYPE html>
<html>
<head>
<title>Sample 03</title>
<script type="text/javascript" src="dependencies/jquery-1.7.1.js"></script>
<script type="text/javascript" src="dependencies/underscore-1.3.1.js"></script>
<script type="text/javascript" src="dependencies/backbone.js"></script>
<script type="text/javascript" src="dependencies/handlebars-1.0.0.beta.6.js"></script>
<script type="text/javascript" src="dependencies/namespace.js"></script>
<script type="text/javascript" src="handlebars-backbone.js"></script>
<script type="text/javascript">
var model = "original model value";
namespace("my.app.models", function(model){
model("Sculpture", {
name: 'Sculpture Name',
img: ''
});
model("Person", {
name: 'Michelangelo',
bornYear: 1475,
diedYear: 1564,
aged: function(){
return this.diedYear - this.bornYear;
},
bestSculpture: null
});
});
namespace("my.app.views", function(model, view){
view('PersonView', {
initialize: function(){
_.bindAll(this, "render");
this.template = $("#templateData");
},
handleWidget: {
bestSculpture: {
widget: 'select',
source: function(){
var Sculpture = my.app.models.Sculpture;
var pieta = new Sculpture({id: 1, name: 'Pieta', img: "http://upload.wikimedia.org/wikipedia/commons/8/8a/Michelangelo%27s_Pieta_5450_cropncleaned.jpg"});
var david = new Sculpture({id: 2, name: 'David', img: "http://upload.wikimedia.org/wikipedia/commons/d/d5/David_von_Michelangelo.jpg"});
return [pieta, david];
}
}
},
render: function () {
this.handleView(this.model);
return this;
}
});
view('PersonResults', {
initialize: function(){
_.bindAll(this, "render");
this.model.bind('change', this.render);
this.template = $("#templateResults");
},
render: function () {
this.handleView(this.model);
return this;
}
});
});
$(function(){
Handlebars.registerHelper('aprox', function(val) {
return "~ " + val;
});
// imports
var Person = my.app.models.Person;
var PersonView = my.app.views.PersonView;
var PersonResults = my.app.views.PersonResults;
var michelangelo = new Person();
new PersonView({el: $("#person"), model: michelangelo}).render();
new PersonResults({el: $("#personResults"), model: michelangelo}).render();
});
</script>
</head>
<body>
<script id="templateData" type="text/x-handlebars-template">
<div><strong>{{name}}</strong></div>
<div>Born: <input name="bornYear" /></div>
<div>
Died:
<select name="diedYear" >
<option value="1563">1563</option>
<option value="1564">1564</option>
<option value="1565">1565</option>
</select>
</div>
<div>Sculpture: {{backHand "bestSculpture"}}</div>
</script>
<script id="templateResults" type="text/x-handlebars-template">
<div>Aged: {{aprox aged}} (calculated)</div>
<div>className: {{className}}</div>
<div>img: <img src="{{bestSculpture.img}}" height="300"/></div>
</script>
<div id="person"></div>
<div id="personResults"></div>
</body>
</html>