/www/javascripts/times.js

https://github.com/le0pard/RWProductManager-iOS · JavaScript · 172 lines · 129 code · 34 blank · 9 comment · 6 complexity · 1e883a2a6c0442910c7cde8afd14cd68 MD5 · raw file

  1. RWManagerTimes = {
  2. Time: null,
  3. TimeList: null,
  4. Times: null,
  5. TimeView: null,
  6. AppView: null,
  7. App: null,
  8. selectedModel: null,
  9. projectsList: null,
  10. init: function(){
  11. RWManagerTimes.Time = Backbone.Model.extend({
  12. name: "time"
  13. });
  14. RWManagerTimes.TimeList = Backbone.Collection.extend({
  15. // Reference to this collection's model.
  16. model: RWManagerTimes.Time,
  17. url: function(){
  18. return 'http://' + RWProductManager.openIdHost + '/api/mobile/time_entries';
  19. }
  20. });
  21. RWManagerTimes.Times = new RWManagerTimes.TimeList;
  22. RWManagerTimes.TimeView = Backbone.View.extend({
  23. tagName: "li",
  24. template: _.template($('#time-template').html()),
  25. events: {
  26. 'tap a' : 'selectedTime'
  27. },
  28. initialize: function() {
  29. this.model.bind('change', this.render, this);
  30. this.model.bind('destroy', this.remove, this);
  31. },
  32. render: function() {
  33. $(this.el).html(this.template(this.model.toJSON())).addClass('arrow');
  34. return this;
  35. },
  36. selectedTime: function(){
  37. RWManagerTimes.selectedModel = this.model;
  38. RWManagerTimes.get_edit_form_data(this.model);
  39. }
  40. });
  41. RWManagerTimes.AppView = Backbone.View.extend({
  42. // Instead of generating a new element, bind to the existing skeleton of
  43. // the App already present in the HTML.
  44. el: $("#times_box"),
  45. // Delegated events for creating new items, and clearing completed ones.
  46. events: {
  47. },
  48. initialize: function() {
  49. RWManagerTimes.Times.bind('add', this.addOne, this);
  50. RWManagerTimes.Times.bind('reset', this.addAll, this);
  51. RWManagerTimes.Times.bind('all', this.render, this);
  52. },
  53. // Re-rendering the App just means refreshing the statistics -- the rest
  54. // of the app doesn't change.
  55. render: function() {
  56. return this;
  57. },
  58. // Add a single todo item to the list by creating a view for it, and
  59. // appending its element to the `<ul>`.
  60. addOne: function(time_model) {
  61. var view = new RWManagerTimes.TimeView({model: time_model});
  62. this.$("#times_list").append(view.render().el);
  63. },
  64. // Add all items in the **Todos** collection at once.
  65. addAll: function() {
  66. RWManagerTimes.Times.each(this.addOne);
  67. }
  68. });
  69. RWManagerTimes.App = new RWManagerTimes.AppView;
  70. },
  71. get_add_form_data: function() {
  72. $.ajax({
  73. url: 'http://' + RWProductManager.openIdHost + '/api/mobile/time_entries/get_accounts_and_categories',
  74. success: function(data){
  75. $('#add_time_accounts').empty();
  76. if (data['accounts']){
  77. var account_options = "";
  78. $.each(data['accounts'], function(key, value) {
  79. account_options += '<option value="' + value['id'] + '">' + value['name'] + '</option>';
  80. });
  81. $('#add_time_accounts').html(account_options);
  82. }
  83. RWManagerTimes.projectsList = data['projects'];
  84. $('#add_time_categories').empty();
  85. if (data['categories']){
  86. var categories_options = "";
  87. $.each(data['categories'], function(key, value) {
  88. categories_options += '<option value="' + value['id'] + '">' + value['name'] + '</option>';
  89. });
  90. $('#add_time_categories').html(categories_options);
  91. }
  92. RWManagerTimes.update_projects_by_account($('#add_time_accounts').val());
  93. }
  94. });
  95. },
  96. get_edit_form_data: function(model) {
  97. $.ajax({
  98. url: 'http://' + RWProductManager.openIdHost + '/api/mobile/time_entries/get_accounts_and_categories',
  99. success: function(data){
  100. $('#time_accounts').empty();
  101. if (data['accounts']){
  102. var account_options = "";
  103. $.each(data['accounts'], function(key, value) {
  104. account_options += '<option value="' + value['id'] + '">' + value['name'] + '</option>';
  105. });
  106. $('#time_accounts').html(account_options);
  107. }
  108. RWManagerTimes.projectsList = data['projects'];
  109. $('#time_categories').empty();
  110. if (data['categories']){
  111. var categories_options = "";
  112. $.each(data['categories'], function(key, value) {
  113. categories_options += '<option value="' + value['id'] + '">' + value['name'] + '</option>';
  114. });
  115. $('#time_categories').html(categories_options);
  116. }
  117. $('#time_id').val(model.get('id'));
  118. $('#time_accounts').val(model.get('project')['account_id']);
  119. $('#time_projects').val(model.get('project_id'));
  120. $('#time_categories').val(model.get('category_id'));
  121. $('#time_hours').val(model.get('hours'));
  122. $('#time_description').val(model.get('description'));
  123. $('#time_date').val(model.get('date'));
  124. RWManagerTimes.update_projects_by_account($('#time_accounts').val());
  125. }
  126. });
  127. },
  128. update_projects_by_account: function(account_id){
  129. $('select.time_projects_list').empty();
  130. if (account_id && RWManagerTimes.projectsList[account_id]){
  131. var projects_options = "";
  132. $.each(RWManagerTimes.projectsList[account_id], function(key, value) {
  133. projects_options += '<option value="' + value['id'] + '">' + value['title'] + '</option>';
  134. });
  135. $('select.time_projects_list').html(projects_options);
  136. }
  137. }
  138. };