-
Notifications
You must be signed in to change notification settings - Fork 1
/
marionette-plusplus.html
170 lines (149 loc) · 5.34 KB
/
marionette-plusplus.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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script src="libs/jquery-1.11.0.min.js"></script>
<script src="libs/lodash.js"></script>
<script src="libs/backbone-min.js"></script>
<script src="libs/backbone.marionette.js"></script>
<link rel="stylesheet" href="style/style.css" />
<title>Théâtre de Guignol</title>
<script type="text/template" id="spectacle-template">
<td><a href="#detail/<%= id %>"><%= titre %></a></td>
<td><%= date %></td>
<td><%= auteur %></td>
<td><%= duree %></td>
<td><%= tarif %></td>
</script>
<script type="text/template" id="programme-template">
<table>
<thead>
<tr> <th>Titre</th> <th>Date</th> <th>Auteur</th><th>Duree</th><th>Tarif</th></tr>
</thead>
<tbody></tbody>
</table>
</script>
<script type="text/template" id="spectacle-detail-template">
<p><a href="#" class="close">< home</a></p>
<h2><%= titre %></h2>
<p><span class="date"><%= date %></span> - <span class="duree"><%= duree %></span></p>
<p><%= description %></p>
<p><span class="auteur">Par <%= auteur %></span> - <span class="tarif">Tarif : <%= tarif %></span></p>
</script>
<script type="text/template" id="spectacle-detail-template2">
<p><a href="#" class="close">< home</a></p>
<h2><%= titre %></h2>
<p><%= description %></p>
<p><span class="date"><%= date %></span> - <span class="duree"><%= duree %></span></p>
<p><span class="auteur">Par <%= auteur %></span> - <span class="tarif">Tarif : <%= tarif %></span></p>
<h2><%= message() %></h2>
</script>
<script>
var Spectacle = Backbone.Model.extend({
idAttribute : "id"
});
var Programme = Backbone.Collection.extend({
url: 'data/programmation.json',
model: Spectacle
});
var SpectacleView = Backbone.Marionette.ItemView.extend({
template: '#spectacle-template',
tagName : 'tr',
modelEvents : {
'change' : 'render'
}
});
var ProgrammeView = Backbone.Marionette.CompositeView.extend({
template: '#programme-template',
itemView : SpectacleView,
itemViewContainer : 'tbody',
modelEvents : {
'sync' : 'render'
}
});
var SpectacleDetailView = Backbone.Marionette.ItemView.extend({
tagName : 'div',
ui : {
'auteur' : '.auteur'
},
events : {
'click .close' : 'closing'
},
modelEvents : {
'change:titre': 'render'
},
templateHelpers: {
message: function(){
return "Eh bien là c'est le template numéro 2 qui utilise des templates helpers!"
}
},
getTemplate: function() {
if (this.model.get("auteur") === "Natalia Poklonskaya"){
return "#spectacle-detail-template2";
} else {
return "#spectacle-detail-template";
}
},
onShow: function () {
console.log("onShow");
},
onBeforeRender: function () {
console.log("onBeforeRender");
},
onRender: function () {
console.log("onRender");
},
onBeforeClose: function () {
console.log("onBeforeClose");
},
onClose: function () {
console.log("onClose");
},
closing : function () {
console.log("closing");
console.log("Text de l'élément auteur : " + this.ui.auteur.text());
this.close();
}
});
var TheatreRouter = Backbone.Router.extend({
routes: {
"": "home",
"detail/:id": "detail"
},
home : function() {
var programmeView = new ProgrammeView({ collection: programme });
theatreApplication.mainRegion.show(programmeView);
},
detail: function(id) {
var spectacle = programme.get(id);
// Ajout dans window
window.spectacle = spectacle;
var spectacleDetailView = new SpectacleDetailView({ model: spectacle });
theatreApplication.mainRegion.show(spectacleDetailView);
}
});
var programme = new Programme();
var theatreApplication = new Backbone.Marionette.Application();
theatreApplication.addInitializer(function () {
theatreApplication.addRegions({
mainRegion: "#content"
});
programme.fetch({
success : function () {
new TheatreRouter();
Backbone.history.start();
}
});
});
window.addEventListener('load', function () {
theatreApplication.start();
});
</script>
</head>
<body>
<div id="main">
<h1 class="marionette">Théatre Marionette</h1>
<div id="content"></div>
</div>
</body>
</html>