/index.php

https://bitbucket.org/od3n/backbone-app-1 · PHP · 243 lines · 193 code · 43 blank · 7 comment · 5 complexity · 70e933ca5754db27de729c28897e0a2e MD5 · raw file

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Sample Backbone App</title>
  5. <link href="/assets/css/bootstrap-combined.min.css" rel="stylesheet">
  6. <script src="/assets/js/jquery.min.js"></script>
  7. <script src="/assets/js/underscore-min.js"></script>
  8. <script src="/assets/js/backbone-min.js"></script>
  9. </head>
  10. <body>
  11. <!-- All underscore templates for simple app-->
  12. <!-- Underscore template for index page -->
  13. <script type="text/template" id="index_template">
  14. <a href="#new" class="btn btn-info">Add New</a>
  15. <hr/>
  16. <table class="table table-bordered">
  17. <thead>
  18. <tr>
  19. <th>Name</th>
  20. <th>Age</th>
  21. <th>Job</th>
  22. <th>Action</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. <% _.each(persons, function(person){ %>
  27. <tr>
  28. <td><%= person.get('name') %></td>
  29. <td><%= person.get('age') %></td>
  30. <td><%= person.get('job') %></td>
  31. <td><a href='#edit/<%= person.get('id') %>' class="btn btn-inverse">Edit</a></td>
  32. </tr>
  33. <% }); %>
  34. </tbody>
  35. </table>
  36. </script>
  37. <!-- Underscore template to add new person -->
  38. <script type="text/template" id="add_person_template">
  39. <form class="form">
  40. <legend>Add New Person</legend>
  41. <label>Name</label>
  42. <input type="text" id="name" />
  43. <label>Age</label>
  44. <input type="text" id="age" />
  45. <label>Job</label>
  46. <input type="text" id="job" />
  47. <hr/>
  48. <input type="button" value="Add" class="btn add" />
  49. <input type="button" value="Cancel" class="btn cancel" />
  50. </form>
  51. </script>
  52. <!-- Underscore template to edit person -->
  53. <script type="text/template" id="edit_person_template">
  54. <form class="form">
  55. <legend>Edit Person Data</legend>
  56. <label>Name</label>
  57. <input type="text" id="name" value="<%= p.get('name') %>" />
  58. <label>Age</label>
  59. <input type="text" id="age" value="<%= p.get('age') %>" />
  60. <label>Job</label>
  61. <input type="text" id="job" value="<%= p.get('job') %>" />
  62. <input type="hidden" id="id" value="<%= p.get('id') %>" />
  63. <hr/>
  64. <input type="button" value="Update" class="btn update" />
  65. <input type="button" value="Delete" class="btn btn-danger delete" />
  66. </form>
  67. </script>
  68. <!-- Main conainer where underscore templates will be loaded -->
  69. <div class="container">
  70. <h1>Person Data System</h1>
  71. <hr/>
  72. <div class="page"></div>
  73. </div>
  74. <!-- Javascrit code. Writing all backbone model, collections and views -->
  75. <script type="text/javascript">
  76. //Backbone Person Modal
  77. Person = Backbone.Model.extend({
  78. urlRoot: 'api.php/person'
  79. });
  80. //Backbone Person Collection
  81. Persons = Backbone.Collection.extend({
  82. url: 'api.php/person'
  83. });
  84. //Backbone Index view, which will load underscore index template
  85. indexView = Backbone.View.extend({
  86. el: $('.page'),
  87. initialize: function(){
  88. this.render();
  89. },
  90. render: function(){
  91. var that = this;
  92. var persons = new Persons();
  93. persons.fetch({
  94. success: function(){
  95. var temp = _.template($('#index_template').html(), {persons: persons.models});
  96. that.$el.html(temp);
  97. }
  98. });
  99. }
  100. });
  101. //Backbone Add Person view, which will load underscore Add Person template
  102. addPersonView = Backbone.View.extend({
  103. el: $('.page'),
  104. initialize: function(){
  105. this.render();
  106. },
  107. render: function(){
  108. var temp = _.template($('#add_person_template').html(), {});
  109. this.$el.html(temp);
  110. },
  111. events:{
  112. 'click .add' : 'addPerson',
  113. 'click .cancel' : 'cancelAddPerson'
  114. },
  115. addPerson: function(){
  116. var name1 = $('#name').val();
  117. var age1 = $('#age').val();
  118. var job1 = $('#job').val();
  119. if(name1.length < 1 || age1.length < 1 || job1.length < 1){
  120. alert('Please fill in form first!');
  121. }else{
  122. var person = new Person({name: name1, age: age1, job: job1});
  123. person.save(null,{
  124. success: function(){
  125. router.navigate('', {trigger:true});
  126. }
  127. });
  128. }
  129. },
  130. cancelAddPerson: function(){
  131. router.navigate('',{trigger:true});
  132. }
  133. });
  134. //Backbone Edit Person view, which will load underscore Edit Person template
  135. editPersonView = Backbone.View.extend({
  136. el: $('.page'),
  137. initialize: function(){
  138. //this.render();
  139. },
  140. render: function(options){
  141. var that = this;
  142. var person = new Person({id: options.id});
  143. person.fetch({
  144. success: function(){
  145. var temp = _.template($('#edit_person_template').html(), {p: person});
  146. that.$el.html(temp);
  147. }
  148. });
  149. },
  150. events:{
  151. 'click .update' : 'updatePerson',
  152. 'click .delete' : 'deletePerson'
  153. },
  154. updatePerson: function(){
  155. var id1 = $('#id').val();
  156. var name1 = $('#name').val();
  157. var age1 = $('#age').val();
  158. var job1 = $('#job').val();
  159. var person = new Person({id: id1, name: name1, age: age1, job: job1});
  160. person.save(null,{
  161. success: function(){
  162. router.navigate('', {trigger:true});
  163. }
  164. });
  165. },
  166. deletePerson: function(){
  167. var id1 = $('#id').val();
  168. var person = new Person({id: id1});
  169. person.destroy({
  170. success: function(){
  171. router.navigate('', {trigger:true});
  172. }
  173. });
  174. }
  175. });
  176. //Backbone Router to handle view navigation in our app
  177. MyRouter = Backbone.Router.extend({
  178. routes:{
  179. '' : 'index',
  180. 'new' : 'addPerson',
  181. 'edit/:id' : 'editPerson'
  182. }
  183. });
  184. var ind = new indexView();
  185. var addP = new addPersonView();
  186. var editP = new editPersonView();
  187. var router = new MyRouter();
  188. router.on('route:index', function(){
  189. ind.render();
  190. });
  191. router.on('route:addPerson', function(){
  192. addP.render();
  193. });
  194. router.on('route:editPerson', function(id){
  195. editP.render({id:id});
  196. });
  197. Backbone.history.start();
  198. </script>
  199. </body>
  200. </html>