-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone-collection.html
85 lines (75 loc) · 2.52 KB
/
backbone-collection.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
<!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>
<script type="text/template" id="articles-collection-template" >
<% _.each(articles, function (article) { %>
<h1><%= article.id %></h1>
<h6><%= article.title %></h6>
<p><%= article.description %></p>
<% }); %>
</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>
var Article = Backbone.Model.extend({
defaults: {
"id": "0",
"title": "Titre par défaut",
"description": "ma description"
},
initialize: function() {
console.log('Here init Article model');
},
});
ArticlesCollection = Backbone.Collection.extend({
model : Article,
initialize : function () {
console.log("Création d’une collection d’articles");
}
});
ArticlesView = Backbone.View.extend({
el: $("#articles-collection-container"),
initialize: function () {
this.template = _.template($("#articles-collection-template").html());
},
render: function () {
var renderedContent = this.template({ articles : this.collection.toJSON() });
$(this.el).html(renderedContent);
return this;
}
});
var articleCollection = new ArticlesCollection;
articleCollection.add( new Article({ id:15, title:"Title 1" }) );
articleCollection.add( new Article({ id:14, title:"Title 2" }) );
articleCollection.add( new Article({ id:16, title:"Title 3" }) );
articleView = new ArticlesView({ collection: articleCollection });
articleView.render();
</script>
</html>