PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/client_app/TodoToDay.js

https://gitlab.com/Dakarlug/backbonejs_tutorial_server_client
JavaScript | 168 lines | 97 code | 31 blank | 40 comment | 2 complexity | 94fff2ff203b9465cf5199e04e91c456 MD5 | raw file
  1. //Mon Model , ToDoToDay
  2. ToDoToDay = Backbone.Model.extend({
  3. initialize: function(options){
  4. //set default value
  5. this.do = options.do || 'Nothing to to'
  6. //set default time
  7. this.time= options.date
  8. this.done= options.do
  9. }
  10. })
  11. //Collection
  12. TodoDodayCollection = Backbone.Collection.extend({
  13. model : ToDoToDay ,
  14. todoSize: function(){
  15. return this.length || 0
  16. },
  17. fetch: function(){
  18. self =this
  19. jQuery.getJSON('http://127.0.0.1:5000').done(function(data){
  20. _.each(data.data, function(data){
  21. //alert(JSON.stringify(data))
  22. self.add(new ToDoToDay(data))
  23. })
  24. }).fail(function(data){
  25. //alert('error')
  26. })
  27. }
  28. })
  29. //Model View
  30. TodoDodayView = Backbone.View.extend({
  31. //You should specify The tagName here , for me I need to create a Table
  32. //So my tag name is tr , backbone will wrap the content with this
  33. //tag Name
  34. //Here we don't need to specify a el , because this is an inner
  35. //view
  36. tagName:"tr",
  37. //My Inner Temple , This is the template for each element , so
  38. // collection render view will iterate over each view
  39. template : '<td>{{do}}</td><td>{{time}}</td><td>{{done}}</td>\
  40. <td><input type ="submit" value ="EditModel" id= "EditModel"/>\
  41. <input type ="submit" value ="DeleteModel" id= "DeleteModel"/></td>\
  42. ',
  43. //Events generated by a ToDoToDay Item view element
  44. events:{
  45. "click #EditModel" :"editModel","click #DeleteModel":"deleteModel",
  46. },
  47. //Initialize your view element
  48. initialize: function(){
  49. //Bind model_change to View
  50. this.model.bind("all", this.render, this)
  51. this.render()
  52. },
  53. //Render view , see backcbone so be sur that you should return
  54. //this for best practic
  55. render: function(){
  56. this.$el.html(Mustache.render(this.template,this.model))
  57. },
  58. //destroy me
  59. deleteModel: function(event){
  60. event.preventDefault()
  61. //this.model.destroy()
  62. //hack m I dont know why this.model.destroy dont work
  63. this.model.collection.remove(this.model)
  64. },
  65. //Edit item , when someone click on buton this fonction will
  66. // fill text input with the current item todo time and description
  67. editModel: function(event){
  68. event.preventDefault()
  69. $("#do").val(this.model.do)
  70. $("#time").val(this.model.time)
  71. // attach model to change to the parent collection
  72. //This is a hack , check backbone for fix
  73. this.model.collection.toUpdate =this.model
  74. }
  75. })
  76. //Collection of item view
  77. TodoDodayCollectionView = Backbone.View.extend({
  78. events:{
  79. "click #delAll":"delAllModel",
  80. "click #addOne":"addOneModel",
  81. "click #updateOne":"updateOne"
  82. },
  83. //Initialise our collection view
  84. initialize: function(){
  85. //Bind model
  86. this.model.fetch()
  87. this.model.bind("all", this.render, this)
  88. this.render()
  89. }
  90. ,
  91. //Mehod To render collection, before rendering elements ,we need
  92. //to empty content of div, to delete the previous render element
  93. render:function(){
  94. var self = this
  95. self.$el.find("#table").empty()
  96. this.model.each(function(_model){
  97. self.$el.find("#table").append(new TodoDodayView(
  98. {model:_model}).$el
  99. )
  100. })
  101. return self
  102. },
  103. // Save model into postgres serveur
  104. saveOneModel: function(model){
  105. jQuery.ajax({
  106. type: "POST",
  107. url : 'http://127.0.0.1:5000/add',
  108. contenType : 'application/json',
  109. dataType :'json',
  110. data: JSON.stringify(model)
  111. }).done(function(data){
  112. //alert('ok')
  113. })
  114. },
  115. // This method allow you to add each element to the collection
  116. addOneModel: function(event){
  117. event.preventDefault()
  118. var toAdd = new ToDoToDay({do: this.$el.find("#do").val(), time: this.$el.find("#time").val()})
  119. this.model.add(toAdd)
  120. this.saveOneModel(toAdd)
  121. },
  122. //destroy collection
  123. delAllModel:function(event){
  124. event.preventDefault()
  125. this.model.reset()
  126. },
  127. //Update One element of the collection
  128. updateOne: function(event){
  129. event.preventDefault()
  130. // remove the olld model attached to this collection
  131. // when used clicked to the Update to do item
  132. // Line 95 in editModel
  133. this.model.remove(this.model.toUpdate)
  134. //Model change
  135. this.model.add({time: this.$el.find("#time").val(), do: this.$el.find("#do").val()})
  136. }
  137. })