-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone-view-events.html
More file actions
148 lines (132 loc) · 4.54 KB
/
Copy pathbackbone-view-events.html
File metadata and controls
148 lines (132 loc) · 4.54 KB
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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Backbone</title>
<!-- === Styles Twitter Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-theme.min.css" rel="stylesheet">
<link href="css/dashboard.css" rel="stylesheet">
<style>
.slack {
background-color: #563A59;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 main">
<h1 class="page-header">Dashboard</h1>
<form id="create-user">
<input type="text" value="" placeholder="Add new user" name="article" />
<button type="submit">Ajouter</button>
</form>
<script type="text/template" id="article-tpl" >
<h1><%= article.id %></h1>
<h6><%= article.title %></h6>
<p><%= article.description %></p>
<button class="edit">Edit</button>
<button class="delete">Delete</button>
</script>
<!-- les données seront affichées ici -->
<div id="articles-collection-container" ></div>
</div>
</div>
</div>
</body>
<!-- === Références aux Frameworks === -->
<script src="js/jquery.js"></script>
<script src="js/underscore.js"></script>
<script src="js/backbone.js"></script>
<script src="js/bootstrap.min.js"></script>
<!-- === ici votre code applicatif === -->
<script>
window.App = {
Models: {},
Collections: {},
Views: {}
};
App.Models.Article = Backbone.Model.extend({
defaults: {
"id": "0",
"title": "Titre par défaut",
"description": "ma description"
},
initialize: function() {
console.log('Here init Article model');
this.on("change:title", function() {
console.log('title has changed : ' + this.get('title'));
});
}
});
App.Collections.Article = Backbone.Collection.extend({
model : App.Models.Article,
initialize : function () {
console.log("Création d’une collection d’articles");
}
});
App.Views.Articles = Backbone.View.extend({
el: "#articles-collection-container",
initialize: function () {
this.collection.on('add', this.printArticle, this);
},
render: function () {
this.collection.each(this.printArticle, this);
},
printArticle: function(article) {
var articleView = new App.Views.Article({ model: article });
$(this.el).append(articleView.render().el);
}
});
App.Views.Article = Backbone.View.extend({
initialize: function () {
this.template = _.template($("#article-tpl").html());
this.model.on('change', this.render, this);
this.model.on('remove', this.remove, this);
},
events: {
'click .edit' : 'editArticle',
'click .delete' : 'deleteArticle'
},
editArticle: function() {
var newTitle = prompt("Please enter the new name", this.model.get('title'));
if( !newTitle ) return;
this.model.set('title', newTitle);
},
deleteArticle: function() {
// Will not trigger sync stuff !!
console.log('Model ' + this.model.get('id') + ' deleted');
this.model.set('id', null);
this.model.destroy();
},
remove: function() {
$(this.el).remove();
},
render: function () {
var renderedContent = this.template({ article : this.model.toJSON() });
$(this.el).html(renderedContent);
return this;
}
});
App.Views.AddUser = Backbone.View.extend({
el: "#create-user",
events: {
"submit": "submit"
},
submit:function(e) {
e.preventDefault();
var articleName = $(e.target).find('input[name="article"]').val();
var article = new App.Models.Article({ title: articleName });
this.collection.add( article );
}
});
var articleCollection = new App.Collections.Article;
articleCollection.add( new App.Models.Article({ id:15, title:"Title 1" }) );
articleCollection.add( new App.Models.Article({ id:14, title:"Title 2" }) );
articleCollection.add( new App.Models.Article({ id:16, title:"Title 3" }) );
articleView = new App.Views.Articles({ collection: articleCollection });
addUserView = new App.Views.AddUser({ collection: articleCollection });
articleView.render();
</script>
</html>