PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/js/dashboard/todos.js

https://gitlab.com/oytunistrator/92five
JavaScript | 105 lines | 105 code | 0 blank | 0 comment | 6 complexity | dd23c278082d10bf60164f8f3b07dd6e MD5 | raw file
  1. (function() {
  2. var ENTER_KEY = 13;
  3. var ESC_KEY = 27;
  4. var TodoModel = Backbone.Model.extend({
  5. urlRoot: 'todo',
  6. toggleStatus: function() {
  7. if (this.get('status') === 'completed') {
  8. this.set({
  9. 'status': 'incomplete'
  10. });
  11. } else {
  12. this.set({
  13. 'status': 'completed'
  14. });
  15. }
  16. this.save();
  17. }
  18. });
  19. var TodoView = Backbone.View.extend({
  20. template: _.template($('#todo-template').html()),
  21. events: {
  22. 'click label': 'toggleStatus'
  23. },
  24. initialize: function() {
  25. this.model.on('change', this.render, this);
  26. },
  27. toggleStatus: function() {
  28. this.model.toggleStatus();
  29. },
  30. render: function() {
  31. this.$el.html(this.template(this.model.toJSON()));
  32. return this;
  33. }
  34. });
  35. var TodoList = Backbone.Collection.extend({
  36. model: TodoModel,
  37. url: 'todo',
  38. initialize: function() {
  39. this.fetch({
  40. reset: true
  41. });
  42. }
  43. });
  44. var TodoListView = Backbone.View.extend({
  45. el: '#todo',
  46. events: {
  47. 'keypress .new-todo': 'createOnEnter',
  48. 'click .del-todo': 'deleteOne'
  49. },
  50. initialize: function() {
  51. _.bindAll(this, 'render');
  52. this.collection.bind('reset', this.render, this);
  53. },
  54. render: function() {
  55. if (this.collection.length === 0) {} else {
  56. this.collection.forEach(this.addOne, this);
  57. }
  58. },
  59. addOne: function(todoItem) {
  60. var todoView = new TodoView({
  61. model: todoItem
  62. });
  63. this.$('#todolist').append(todoView.render().el);
  64. },
  65. createOnEnter: function(e) {
  66. if (e.which === ENTER_KEY && this.$('#new-todo').val().trim()) {
  67. var todoModel = new TodoList();
  68. todoModel.create({
  69. 'text': this.$('#new-todo').val().trim()
  70. }, {
  71. success: function(model, response, options) {
  72. var todoView = new TodoView({
  73. model: response
  74. });
  75. this.$('#todolist').prepend(todoView.render().el);
  76. this.$('#new-todo').val('');
  77. },
  78. error: function(model, xhr, options) {
  79. console.log('error');
  80. }
  81. });
  82. }
  83. },
  84. deleteOne: function(e) {
  85. var todoId = $(e.target).attr('todoid');
  86. var todoModel = new TodoModel();
  87. todoModel.set({
  88. 'id': todoId
  89. });
  90. todoModel.destroy({
  91. success: function(model, response, options) {
  92. $(e.target).parent().parent().parent().parent().remove();
  93. },
  94. error: function(model, xhr, options) {
  95. console.log('error');
  96. }
  97. });
  98. }
  99. });
  100. var todolist = new TodoList();
  101. var todolistView = new TodoListView({
  102. collection: todolist
  103. });
  104. todolistView.render();
  105. }());