-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
148 lines (130 loc) · 4.27 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Backbone Bootstrap</title>
<link href="./css/bootstrap.css" rel="stylesheet">
<link href="./css/bootstrap-responsive.css" rel="stylesheet">
<link href="./css/style.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Libraries: -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function() {
window.App = {};
//MODEL
TodoItem = App.TodoItem = Backbone.Model.extend({
toggleStatus: function(){
if(this.get('status') === 'incomplete'){
this.set({'status': 'complete'});
}else{
this.set({'status': 'incomplete'});
}
this.save();
}
});
//MODEL INSTANCE
todoItem = App.todoItem = new TodoItem();
//COLLECTION
TodoList = App.TodoList = Backbone.Collection.extend({
//url: '/todos'
model: TodoItem,
initialize: function(){
this.on('remove', this.hideModel);
},
hideModel: function(model){
model.trigger('hide');
}
});
//COLLECTION INSTANCE
var todoList = App.todoList = new TodoList();
todoList.reset([
{ description: 'Learn Backbone.js', status: 'complete'},
{ description: 'Get into CoffeeScript', status: 'incomplete'},
{ description: 'Build an App', status: 'incomplete'},
]);
todoList.forEach(function(todoItem){
console.log(todoItem.get('description'));
});
//VIEW
var TodoView = App.TodoView = Backbone.View.extend({
id: 'todo-view',
template: _.template($("#todoTemplate").html()),
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this.$el;
},
events: {'change input': 'toggleStatus'},
toggleStatus: function(){
this.model.toggleStatus();
this.render();
},
initialize: function(){
this.model.on('change', this.render, this);
this.model.on('destroy', this.remove, this);
}
});
//COLLECTION VIEW
var TodoListView = App.TodoListView = Backbone.View.extend({
initialize: function(){
this.collection.on('add', this.addOne, this);
this.collection.on('reset', this.addAll, this);
},
addOne: function(todoItem){
var todoView = App.todoView = new TodoView({model: todoItem});
this.$el.append(todoView.render());
},
render: function(){
this.addAll();
return this.$el;
},
addAll: function(){
this.collection.forEach(this.addOne, this);
return this.$el;
},
});
var todoListView = App.todoListView = new TodoListView({collection: todoList});
todoListView.render().appendTo('body');
});
</script>
<div class="navbar navbar-inverse">
<div class="navbar-inner">
<div class="container">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<!-- Be sure to leave the brand out there if you want it shown -->
<a class="brand" href="#">Backbone Sandbox</a>
<!-- Everything you want hidden at 940px or less, place within here -->
<div class="nav-collapse">
<!-- .nav, .navbar-search, .navbar-form, etc -->
<form class="navbar-search pull-left">
<input type="text" class="search-query" placeholder="Search">
</form>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span2">
<script type="text/template" id="todoTemplate">
<h3 class='<%= status %>'><input type=checkbox <% if(status === "complete") print("checked") %>/> <%= description %></h3>
</script>
</div>
</div>
</div>
<script src="js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>