|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 5 | + <title>Backbone</title> |
| 6 | + <!-- === Styles Twitter Bootstrap --> |
| 7 | + <link href="css/bootstrap.min.css" rel="stylesheet"> |
| 8 | + <link href="css/bootstrap-theme.min.css" rel="stylesheet"> |
| 9 | + <link href="css/dashboard.css" rel="stylesheet"> |
| 10 | + <style> |
| 11 | + .slack { |
| 12 | + background-color: #563A59; |
| 13 | + } |
| 14 | + </style> |
| 15 | + </head> |
| 16 | + <body> |
| 17 | + <div class="container-fluid"> |
| 18 | + <div class="row"> |
| 19 | + <div class="col-sm-12 main"> |
| 20 | + <h1 class="page-header">Dashboard</h1> |
| 21 | + <form id="create-user"> |
| 22 | + <input type="text" value="" placeholder="Add new user" name="article" /> |
| 23 | + <button type="submit">Ajouter</button> |
| 24 | + </form> |
| 25 | + <script type="text/template" id="article-tpl" > |
| 26 | + <h1><%= article.id %></h1> |
| 27 | + <h6><%= article.title %></h6> |
| 28 | + <p><%= article.description %></p> |
| 29 | + <button class="edit">Edit</button> |
| 30 | + <button class="delete">Delete</button> |
| 31 | + </script> |
| 32 | + <!-- les données seront affichées ici --> |
| 33 | + <div id="articles-collection-container" ></div> |
| 34 | + </div> |
| 35 | + </div> |
| 36 | + </div> |
| 37 | + </body> |
| 38 | + <!-- === Références aux Frameworks === --> |
| 39 | + <script src="js/jquery.js"></script> |
| 40 | + <script src="js/underscore.js"></script> |
| 41 | + <script src="js/backbone.js"></script> |
| 42 | + <script src="js/bootstrap.min.js"></script> |
| 43 | + <!-- === ici votre code applicatif === --> |
| 44 | + <script> |
| 45 | + window.App = { |
| 46 | + Models: {}, |
| 47 | + Collections: {}, |
| 48 | + Views: {} |
| 49 | + }; |
| 50 | + |
| 51 | + App.Models.Article = Backbone.Model.extend({ |
| 52 | + defaults: { |
| 53 | + "id": "0", |
| 54 | + "title": "Titre par défaut", |
| 55 | + "description": "ma description" |
| 56 | + }, |
| 57 | + |
| 58 | + initialize: function() { |
| 59 | + console.log('Here init Article model'); |
| 60 | + this.on("change:title", function() { |
| 61 | + console.log('title has changed : ' + this.get('title')); |
| 62 | + }); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + App.Collections.Article = Backbone.Collection.extend({ |
| 67 | + model : App.Models.Article, |
| 68 | + initialize : function () { |
| 69 | + console.log("Création d’une collection d’articles"); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + App.Views.Articles = Backbone.View.extend({ |
| 74 | + el: "#articles-collection-container", |
| 75 | + initialize: function () { |
| 76 | + this.collection.on('add', this.printArticle, this); |
| 77 | + }, |
| 78 | + |
| 79 | + render: function () { |
| 80 | + this.collection.each(this.printArticle, this); |
| 81 | + }, |
| 82 | + |
| 83 | + printArticle: function(article) { |
| 84 | + var articleView = new App.Views.Article({ model: article }); |
| 85 | + $(this.el).append(articleView.render().el); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + App.Views.Article = Backbone.View.extend({ |
| 90 | + initialize: function () { |
| 91 | + this.template = _.template($("#article-tpl").html()); |
| 92 | + this.model.on('change', this.render, this); |
| 93 | + this.model.on('remove', this.remove, this); |
| 94 | + }, |
| 95 | + |
| 96 | + events: { |
| 97 | + 'click .edit' : 'editArticle', |
| 98 | + 'click .delete' : 'deleteArticle' |
| 99 | + }, |
| 100 | + |
| 101 | + editArticle: function() { |
| 102 | + var newTitle = prompt("Please enter the new name", this.model.get('title')); |
| 103 | + if( !newTitle ) return; |
| 104 | + this.model.set('title', newTitle); |
| 105 | + }, |
| 106 | + |
| 107 | + deleteArticle: function() { |
| 108 | + // Will not trigger sync stuff !! |
| 109 | + console.log('Model ' + this.model.get('id') + ' deleted'); |
| 110 | + this.model.set('id', null); |
| 111 | + this.model.destroy(); |
| 112 | + }, |
| 113 | + |
| 114 | + remove: function() { |
| 115 | + $(this.el).remove(); |
| 116 | + }, |
| 117 | + |
| 118 | + render: function () { |
| 119 | + var renderedContent = this.template({ article : this.model.toJSON() }); |
| 120 | + $(this.el).html(renderedContent); |
| 121 | + return this; |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + App.Views.AddUser = Backbone.View.extend({ |
| 126 | + el: "#create-user", |
| 127 | + events: { |
| 128 | + "submit": "submit" |
| 129 | + }, |
| 130 | + |
| 131 | + submit:function(e) { |
| 132 | + e.preventDefault(); |
| 133 | + var articleName = $(e.target).find('input[name="article"]').val(); |
| 134 | + var article = new App.Models.Article({ title: articleName }); |
| 135 | + this.collection.add( article ); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + var articleCollection = new App.Collections.Article; |
| 140 | + articleCollection.add( new App.Models.Article({ id:15, title:"Title 1" }) ); |
| 141 | + articleCollection.add( new App.Models.Article({ id:14, title:"Title 2" }) ); |
| 142 | + articleCollection.add( new App.Models.Article({ id:16, title:"Title 3" }) ); |
| 143 | + |
| 144 | + articleView = new App.Views.Articles({ collection: articleCollection }); |
| 145 | + addUserView = new App.Views.AddUser({ collection: articleCollection }); |
| 146 | + articleView.render(); |
| 147 | + </script> |
| 148 | +</html> |
0 commit comments