forked from joaool/MotherGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone_menu3.html
264 lines (259 loc) · 9.18 KB
/
backbone_menu3.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Backbone/menu v3.0</title>
<!-- Bootstrap core CSS -->
<!--
<link href="../smartmenus-master/src/libs/demo-assets/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="../bootstrap/css/cosmos.css" rel="stylesheet">
<link href="../bootstrap/css/readable.css" rel="stylesheet">
<link href="../bootstrap/css/spacelab.css" rel="stylesheet">
<link href="../bootstrap/css/amelia.css" rel="stylesheet">
-->
<link href="../bootstrap/css/cerulean.css" rel="stylesheet">
<!-- SmartMenus jQuery Bootstrap Addon CSS -->
<!--
-->
<link href="../smartmenus-master/src/addons/bootstrap/jquery.smartmenus.bootstrap.css" rel="stylesheet">
</head>
<body style="padding-top:40px;">
<p><strong>FrameLink Menus: Bootstrap with smartmenus addon and Backbone. All menus from json. CSS is bootstrap bootswatch</strong></p>
<div id="xcontainer">
<button>Load</button>
<ul id="list">
</ul>
</div>
<div id="list-template">
<li><a href=""></a></li>
</div>
<div id="submenu-template">
<li><a href=""></a>
<ul class="dropdown-menu">
<li><a href="#">Item 2-2-1</a></li>
</ul>
</li>
</div>
<div class="container">
<div class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="./index.html"><img style="height:55px;" src="../bootstrap/img/framelink-logoWithText.png"></a>
</div>
<div class="navbar-collapse collapse">
<ul id="begin_menu" class="nav navbar-nav">
</ul>
</div>
</div>
</div>
<!--
<script type="text/javascript" charset="utf-8" src="../rivets/dist/rivets.js" ></script>
-->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="../smartmenus-master/src/libs/demo-assets/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../smartmenus-master/src/jquery.smartmenus.js"></script>
<script type="text/javascript" src="../smartmenus-master/src/addons/bootstrap/jquery.smartmenus.bootstrap.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
<script type="text/javascript">
var oMenu = {
"menu" : [
{
"title" : "Menu A - Tier 1",
"uri":"http://www.microsoft.com"
},
{
"title" : "Menu B - Tier 1",
"uri" : "#",
"menu" : [
{
"title" : "Menu B - Tier 2 - I ",
"uri":"#",
"menu" : [
{
"title" : "Menu B - Tier 3 - I FrameLink",
"uri":"http://framelink.co"
},
{
"title" : "Menu B - Tier 3 - II",
"uri":"#",
"menu" : [
{
"title" : "Menu B - Tier 3 - I FrameLink",
"uri":"http://framelink.co"
},
{
"title" : "Menu B - Tier 3 - II",
"uri":"#",
"menu" : [
{
"title" : "Menu B - Tier 4 - I FrameLink",
"uri":"http://framelink.co"
},
{
"title" : "Menu B - Tier 4 - II",
"uri":"#"
},
{
"title" : "Menu B - Tier 4 - III weAdvice",
"uri":"http://weadvice.pt"
}
]
},
{
"title" : "Menu B - Tier 3 - III weAdvice",
"uri":"http://weadvice.pt"
}
]
}
]
},
{
"title" : "Menu B - Tier 2 - II",
"uri":"#"
}
]
},
{
"title" : "Menu C - Tier 1",
"uri":"#3",
},
{
"title" : "Menu D - Tier 1",
"uri":"#3",
"menu" : [
{
"title" : "Menu D - Tier 2 - I",
"uri":"#1"
},
{
"title" : "Menu D - Tier 2 - II",
"uri":"#",
"menu" : [
{
"title" : "Menu D - Tier 3 - I FrameLink",
"uri":"http://framelink.co"
},
{
"title" : "Menu D - Tier 3 - II",
"uri":"#"
},
{
"title" : "Menu D - Tier 3 - III weAdvice",
"uri":"http://weadvice.pt"
}
]
},
{
"title" : "Menu D - Tier 2 - III - facebook",
"uri":"http://facebook.com"
}
]
}
]
};
var abc=_.template('<p><%= text %></p>', {text: 'Hello underscore'});
// $("body").html(abc);//body becomes html(abc)
$(abc).insertBefore("#xcontainer");
model = new Backbone.Model({
data:[
{text:"Google",href:"http://google.com"},
{text:"Facebook",href:"http://facebook.com"},
{text:"YouTube",href:"http://youtube.com"}
]
});
var View = Backbone.View.extend({
initialize:function(){
console.log("Initializing view with..." + this.options.JOBlankOption );
this.xtemplate = $("#list-template").children(); //we want the li line.
},
el:"#xcontainer", //the prebuild option is used here meaning that this view always refers to #container
events:{
"click button":"render" //this will trigger button in container, outside the container will not receive the event...
},
render:function(){
var data = this.model.get("data");
for (var i=0; i<data.length; i++) {
console.log("line= "+i+" "+data[i].text);
var li = this.xtemplate.clone().find("a").attr("href",data[i].href).text(data[i].text).end();
this.$el.find("ul").append(li);
}
// alert("JO");
return null;
}
});
var view = new View({model:model,JOBlankOption:"empty string"});
view.render();
Menu = Backbone.Model.extend({
initialize: function(){
console.log("Initializing menu");
},
defaults:{
title:null,
uri:null,
menu:null
}
});
var mainMenu = new Menu();
var Menus = Backbone.Collection.extend({
model:Menu
});
var menus = new Menus;//menus is a collection of Menu models
for(var i=0;i<oMenu.menu.length;i++){
// console.log(i+" --->"+oMenu.menu[i].title);
var menuModel = new Menu({title:oMenu.menu[i].title, uri:oMenu.menu[i].uri,menu:oMenu.menu[i].menu});
menus.add(menuModel);
}
// console.log(menus.toJSON());//this shows all models that are in the collection...
// menus.each(function(model){
// console.log("---->"+model.get("title"));
// });
menuHTML = function(menuItem){
// now we clone the template, and manipulate it finding element "a" and changing attribute "href" to the uri and change the text in the element to title - after that we do an end in order to get back to the li element
// var htmlToAppend =listTemplate.clone().find("a").attr("href",menuItem.uri).text(menuItem.title).end();
var htmlStr = "<li><a href=" + menuItem.uri + ">" + menuItem.title + "</a></li>";
if(menuItem.menu){
var htmlStr = "<li><a href=" + menuItem.uri + ">" + menuItem.title + "</a>";
htmlStr += "<ul class='dropdown-menu'>";
var subMenuItem = menuItem.menu;
for (var i=0;i<subMenuItem.length;i++){
// htmlStr += "<li><a href=" + subMenuItem[i].uri + ">" + subMenuItem[i].title + "</a></li>";
htmlStr += menuHTML(subMenuItem[i])
}
htmlStr += "</ul>";
htmlStr += "</li>";
}
// htmlToAppend = $(htmlStr);//we convert htmlStr to a JQuery object
// console.log("menuToDOM htmlToAppend =================>"+htmlToAppend.html());
// console.log("menuToDOM this="+this);
return htmlStr;
};
var MenuView = Backbone.View.extend({
initialize: function(){
console.log("Initializing menu view "+this.el);
this.render();
},
el: "#begin_menu",
render: function(){
var data = menus.toJSON();
// console.log("Total to Render ====>"+JSON.stringify(data));
for (var i=0;i<data.length;i++){
console.log("Render menu ====>"+data[i].title);
this.$el.append( $( menuHTML( data[i] ) ) );//with $(htmlStr) we convert htmlStr to a JQuery object
}
}
});
// var menuView = new MenuView({el:document.getElementById("begin_menu")});
var menuView = new MenuView({model:model});
console.log(document.title+"...... END..");
</script>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
</body>
</html>