forked from BorisMoore/jsviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_accordion_switching-template.html
114 lines (98 loc) · 2.96 KB
/
06_accordion_switching-template.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
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.2.js" type="text/javascript"></script>
<script src="../../jsrender.js" type="text/javascript"></script>
<script src="../../jquery.observable.js" type="text/javascript"></script>
<script src="../../jquery.views.js" type="text/javascript"></script>
<link href="../resources/demos.css" rel="stylesheet" type="text/css" />
<style type="text/css">
table { cursor:pointer; border-collapse:collapse; border:2px solid blue; width:300px; margin:8px; }
table tr { border:1px solid blue; color:blue; background-color:#f8f8f8; } table td { padding:3px; } table tr:hover { color:red; }
.movieDetail { background-color:yellow; } .movieDetail.row1 { border-bottom:none; } .movieDetail.row2 { border-top:none; }
</style>
</head>
<body>
<div class="nav"><a href="../demos.html">JsViews Demos</a></div>
<h3>Accordion: Using dynamic switching of templates</h3>
<script id="summaryTemplate" type="text/x-jsrender">
<tr class='movieSummary'><td>
{{>title}}
</td></tr>
</script>
<script id="detailTemplate" type="text/x-jsrender">
<tr class='movieDetail row1'><td>
{{>title}}
</td></tr>
<tr class='movieDetail row2'><td><b>Languages:</b>
{{for languages}}
<div>
<em>{{>name}}</em>
</div>
{{/for}}
</td></tr>
</script>
Click for details:
<div class="height">
<table><tbody id="movieList"></tbody></table>
</div>
<script type="text/javascript">
var selectedSubView = null,
movies = [
{
title: "The Red Violin",
languages: [
{ name: "English" },
{ name: "French" }
]
},
{
title: "Eyes Wide Shut",
languages: [
{ name: "French" },
{ name: "German" },
{ name: "Spanish" }
]
},
{
title: "The Inheritance",
releaseYear: "1976",
languages: [
{ name: "English" },
{ name: "Dutch" }
]
}
];
$.templates({
detailTemplate: "#detailTemplate",
summaryTemplate: "#summaryTemplate"
});
function unselect() {
/* Switch template of the selected view back to the summary template */
if ( selectedSubView ) {
selectedSubView.tmpl = $.templates.summaryTemplate;
selectedSubView.refresh();
selectedSubView = null;
}
}
/* Render the movies array as data-linked content under the movieList <ul>, using the summaryTemplate */
$.link.summaryTemplate( "#movieList", movies )
/* onclick handler for movie subviews when not selected */
.on( "click", ".movieSummary", function() {
/* Unselect the currently selected view */
unselect();
/* Get the view which this clicked element
belongs to, and make it the selected view */
selectedSubView = $.view(this);
/* Switch the template on this view to the detail template */
selectedSubView.tmpl = $.templates.detailTemplate;
selectedSubView.refresh();
})
/* onclick handler for movie subviews when selected */
.on( "click", ".movieDetail", function() {
/* Unselect the currently selected view */
unselect();
});
</script>
</body>
</html>