forked from joaool/MotherGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone_menu.html
231 lines (222 loc) · 8.29 KB
/
backbone_menu.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Backbone/menu v1.0</title>
<!-- Bootstrap core CSS -->
<!--
<link href="../smartmenus-master/src/libs/demo-assets/bootstrap/css/bootstrap.min.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. 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">
<li><a href="#">FrameLink</a></li>
<li><a href="#">Item 2</a>
<ul class="dropdown-menu">
<li><a href="#">Item 2-1</a></li>
<li><a href="#">Item 2-2</a>
<ul class="dropdown-menu">
<li><a href="#">Item 2-2-1</a></li>
<li><a href="#">Item 2-2-2</a></li>
<li><a href="#">Item 2-2-3</a>
<ul class="dropdown-menu">
<li><a href="http://microsoft.com">Microsoft</a></li>
<li><a href="#">Item 2-2-3-2</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Item 2-3</a></li>
</ul>
</li>
<li><a href="#">Item 3</a></li>
</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":"#1"
},
{
"title" : "Menu B - Tier 2 - II",
"uri":"#"
}
]
},
{
"title" : "Menu C - Tier 1",
"uri":"#3",
},
{
"title" : "Menu D - Tier 1",
"uri":"#3",
"menu":"hjhj"
}
]
};
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...
// var i =0;
menus.each(function(model){
// console.log("%%%%%%%%%zzz%%%%%%%%%%->"+JSON.stringify(model.toJSON())); //OK
// console.log("---->"+model.toJSON().title);//OK
console.log("---->"+model.get("title"));
});
menuToDOM = function(menuItem,listTemplate,subMenuTemplate){
// 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();
if(menuItem.menu){
htmlToAppend = subMenuTemplate.clone().find("a").attr("href","#xxx").text(menuItem.title).end();
// var rawHTML = subMenuTemplate.html();
// var insideUL = $("#submenu-template ul").children();//inside id gets all ul.children ex:<a href="#">Item 2-2-1</a>
var insideUL = $("#submenu-template").find("ul");//inside id gets all ul ex:<li><a href="#">Item 2-2-1</a></li>
console.log("menuToDOM insideUL =================>"+rawHTML);
var rawHTML = insideUL.html(); //ex <a href="#">Item 2-2-1</a>
var htmlStr = "<li><a href=" + menuItem.uri + ">" + menuItem.title + "</a>";
htmlStr += "<ul class='dropdown-menu'>";
htmlStr += "<li><a href='#'>Item 2-2-1</a></li>";
htmlStr += "</ul>";
htmlStr += "</li>";
console.log("menuToDOM insideUL =================>"+htmlStr);
htmlToAppend = $(htmlStr);//we convert htmlStr to a JQuery object
}
console.log("menuToDOM htmlToAppend =================>"+htmlToAppend.html());
// console.log("menuToDOM this="+this);
return htmlToAppend;
};
var MenuView = Backbone.View.extend({
initialize: function(){
console.log("Initializing menu view "+this.el);
this.template = $("#list-template").children();//this template will be copied over and over in render...
this.subTemplate = $("#submenu-template").children();
this.render();
},
el: "#begin_menu",
render: function(){
var data = menus.toJSON();
console.log("Total to Render ====>"+JSON.stringify(data));
// console.log("sub template =================>"+this.subTemplate.html());
// alert(typeof window.menuToDOM);
for (var i=0;i<data.length;i++){
console.log("Render menu ====>"+data[i].title);
this.$el.append(menuToDOM(data[i],this.template,this.subTemplate));
}
}
});
// 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 -->
<!--
<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>
-->
<!-- SmartMenus jQuery plugin -->
<!-- SmartMenus jQuery Bootstrap Addon -->
</body>
</html>