PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/templates/backbone-products.html

https://gitlab.com/rakeshviyak/dimest
HTML | 259 lines | 202 code | 54 blank | 3 comment | 0 complexity | a5d11a094135a3eead9bcf7f266d4959 MD5 | raw file
  1. {% extends 'index.html' %}
  2. {% block backbone %}
  3. <script src="//cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.min.js"></script>
  4. <script src="/static/js/underscore-1.5.2-min.js"></script>
  5. <script src="/static/js/backbone-1.1-min.js"></script>
  6. <script>
  7. (function($){
  8. //Create a model
  9. Product = Backbone.Model.extend({
  10. idAttribute:'_id',
  11. });
  12. //Create a collection
  13. Products = Backbone.Collection.extend({
  14. Model: Product,
  15. url: "#"
  16. });
  17. //Initiate a collection
  18. products = new Products();
  19. //Define Home View
  20. HomeView = Backbone.View.extend({
  21. el: 'body',
  22. initialize: function() {
  23. this.template = _.template($('#item-home').html());
  24. this.render();
  25. },
  26. render: function() {
  27. var el = this.$el
  28. el.empty();
  29. el.append(this.template());
  30. var listView = new ListView({collection:products});
  31. $('.products_table').append(listView.render().el);
  32. return this;
  33. }
  34. });
  35. //Define List View -List all products in the collection
  36. ListView = Backbone.View.extend({
  37. tagName: 'table',
  38. className : 'products_table table',
  39. initialize: function() {
  40. this.collection.bind('all', _.bind(this.render,this));
  41. this.template = _.template($('#item-list').html());
  42. },
  43. events: {
  44. "click .remove":"remove",
  45. "click .edit":"edit"
  46. },
  47. remove:function(e){
  48. e.preventDefault();
  49. var id = $(e.currentTarget).data("id");
  50. var item = this.collection.get(id);
  51. this.collection.remove(item);
  52. return this;
  53. },
  54. edit:function(e){
  55. e.preventDefault();
  56. var id = $(e.currentTarget).data("id");
  57. var item = this.collection.get(id);
  58. $('.form-horizontal').empty();
  59. this.collection.remove(item);
  60. new AddView();
  61. },
  62. render:function (eventName) {
  63. var template = this.template,
  64. el = this.$el;
  65. $("table tr").remove();
  66. this.collection.each(function(product){
  67. j=product.toJSON();
  68. j['id']=product.cid;
  69. console.log(template);
  70. el.append(template(j));
  71. });
  72. return this;
  73. }
  74. });
  75. //Define AddView - to add new products to the collection
  76. AddView = Backbone.View.extend({
  77. className:"span4",
  78. tagName: "div",
  79. events: {
  80. "click #addBtn": "add",
  81. "keypress .addName": "onEnter"
  82. },
  83. initialize: function() {
  84. this.template = _.template($('#item-edit').html());
  85. this.render();
  86. },
  87. render: function() {
  88. $(this.el).html(this.template());
  89. $('.row').append(this.el);
  90. return this;
  91. },
  92. onEnter: function(e) {
  93. if (e.keyCode == 13) {
  94. console.log("on Enter")
  95. this.add(e);
  96. }
  97. },
  98. add: function(e) {
  99. e.preventDefault();
  100. var newProduct = new Product({name:$('#name').val(),type:$('#type').val() });
  101. products.add(newProduct);
  102. console.log(products);
  103. $('#name').val('');
  104. return this;
  105. }
  106. });
  107. //Define Router. List to two routes, home and add view
  108. AppRouter = Backbone.Router.extend({
  109. routes:{
  110. "":"home",
  111. "add":"add",
  112. },
  113. home:function() {
  114. console.log('home');
  115. new HomeView();
  116. },
  117. add:function() {
  118. console.log('add');
  119. new AddView();
  120. },
  121. });
  122. }(jQuery));
  123. $(document).ready(function () {
  124. //initiate the router
  125. productApp = new AppRouter();
  126. //keeps the browser history in memory
  127. Backbone.history.start();
  128. //initiate new products when DOM runs
  129. var p = new Products([
  130. {type: "Music", name: "Unit"},
  131. {type: "Video", name: "EDG"},
  132. {type: "Music", name: "Plateia"}
  133. ]);
  134. products.add(p.toJSON());
  135. });
  136. </script>
  137. <!-- Define template for the home view -->
  138. <script type="text/template" id="item-home">
  139. <div class="container">
  140. <div class="rows">
  141. <div class="col-md-6">
  142. <div class="base_border">
  143. <div class="base">
  144. <div class="products_search">My Recently added Products:</div>
  145. <table class="products_table table">
  146. </table>
  147. </div>
  148. </div>
  149. </div>
  150. </div>
  151. <div class="col-md-6">
  152. <div class="row">
  153. <div class="span4">
  154. <div class="content upload_new" style="margin-bottom: 20px"><a href="#add">upload new products &gt; </a></div>
  155. </div>
  156. </div>
  157. </div>
  158. </div>
  159. </div>
  160. </script>
  161. <!-- Define template for the add view (to add in new products) -->
  162. <script type="text/template" id="item-edit">
  163. <form class="form-horizontal" method="post" role="form" style="margin-left:20px">
  164. <legend>Enter the product detail:</legend>
  165. <div class="form-group">
  166. <label class="control-label col-md-3" for="name">Name:</label>
  167. <div class="col-md-6">
  168. <input type="text" class="addName form-control" id="name" name="name" placeholder="Enter the product name">
  169. </div>
  170. </div>
  171. <div class="form-group ">
  172. <select id="type" class="col-md-offset-4 addName">
  173. <option>Music</option>
  174. <option>Video</option>
  175. <option>E-book</option>
  176. <option>Other</option>
  177. </select>
  178. </div>
  179. <div class="form-group">
  180. <div class="controls">
  181. <button type="submit" class="col-md-3 col-md-offset-2"><a href="#" id="cancelBtn" class="btn">cancel</a></button>
  182. <button type="submit" class="col-md-3 addName"><a href="#" id="addBtn" class="btn addName">add</a></button>
  183. </div>
  184. </div>
  185. </form>
  186. </script>
  187. <!-- Define template for the list view (product list) -->
  188. <script type="text/template" id="item-list">
  189. <tr class="product">
  190. <td>
  191. <img class="product_img" src="/static/img/i_p2_tile.jpg">
  192. <span><%= name %></span>
  193. </td>
  194. <td>
  195. <img src="/static/img/i_video.png">
  196. <span><%= type %></span>
  197. </td>
  198. <td>
  199. <a href="#" data-id="<%= id %>" class="edit" data-toggle="tooltip" title="edit">
  200. <img src="/static/img/i_edit.png" alt="edit" style="padding-right:10px"></a>
  201. <a href="#" data-id="<%= id %>" class="remove" data-toggle="tooltip" title="delete"><img src="/static/img/i_delete.png" alt="Delete" style="padding-right:10px"></a></td>
  202. </tr>
  203. <tr class="spacer"><td></td></tr>
  204. </script>
  205. {% endblock %}