PageRenderTime 158ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/public/js/views/bank_transactions/index.js

https://github.com/bozz/cashflow
JavaScript | 113 lines | 86 code | 24 blank | 3 comment | 1 complexity | 2a935536b579c1940218b839d82b8090 MD5 | raw file
  1. define(function(require) {
  2. var utils = require('utils');
  3. var tpl = require('text!/templates/bank_transactions/index.jst.ejs');
  4. var BankTransaction = require('models/BankTransaction');
  5. var BankTransactions = require('collections/BankTransactions');
  6. var BankTransactionView = require('views/bank_transactions/detail');
  7. var PaginationView = require('views/misc/pagination');
  8. var accounting = require('accounting');
  9. var BankTransactionListView = Backbone.View.extend({
  10. // ID of current bank_account
  11. bankId: false,
  12. template: _.template(tpl),
  13. events: {
  14. 'click button.btn-new': 'newTransaction',
  15. 'click button.btn-edit': 'editTransaction',
  16. 'click button.btn-delete': 'deleteTransaction',
  17. 'submit form.form-search': 'filterTransactions'
  18. },
  19. initialize: function(config) {
  20. this.parentView = config.parentView;
  21. this.bankId = config.bankId;
  22. this.collection = new BankTransactions({bankId: this.bankId});
  23. this.collection.pager();
  24. this.collection.on('reset', this.render, this);
  25. this.collection.on('add', this.render, this);
  26. this.collection.on('change', this.render, this);
  27. this.collection.on('destroy', this.render, this);
  28. this.paginationView = new PaginationView({
  29. collection: this.collection,
  30. parentView: this
  31. });
  32. },
  33. close: function() {
  34. this.remove();
  35. this.unbind();
  36. this.collection.off('reset', this.render);
  37. this.collection.off('add', this.render);
  38. this.collection.off('change', this.render);
  39. this.collection.off('destroy', this.render);
  40. },
  41. render: function() {
  42. this.$el.html(this.template({
  43. bankId: this.bankId,
  44. transactions: this.collection,
  45. accounting: accounting
  46. }));
  47. // set any previous filter values
  48. // TODO: extract filter form into seperate template
  49. this.$el.find('form.form-search input.search-query').val(this.collection.server_api.q);
  50. this.paginationView.render();
  51. this.delegateEvents();
  52. return this;
  53. },
  54. newTransaction: function(event) {
  55. var model = new BankTransaction();
  56. this.showTransactionsDetail(model);
  57. },
  58. editTransaction: function(event) {
  59. var id = $(event.currentTarget).attr('value');
  60. var model = this.collection.get(id);
  61. this.showTransactionsDetail(model);
  62. },
  63. showTransactionsDetail: function(model) {
  64. event.preventDefault();
  65. view = new TransactionView({bankId: this.bankId, model: model});
  66. $('#content').append(view.render().el);
  67. $('#transaction-modal').modal();
  68. },
  69. deleteTransaction: function(event) {
  70. var id = $(event.currentTarget).attr('value');
  71. var model = this.collection.get(id);
  72. bootbox.confirm("Do you really want to delete this transaction?", "Cancel", "Confirm Delete", function(result) {
  73. if (result) {
  74. model.destroy({
  75. success: function(model, response) {
  76. utils.alertSuccess('Item deleted successfully');
  77. }
  78. });
  79. }
  80. });
  81. },
  82. filterTransactions: function(event) {
  83. event.preventDefault();
  84. var q = this.$el.find('form.form-search input.search-query').val();
  85. this.collection.applyFilter(q);
  86. }
  87. });
  88. return BankTransactionListView;
  89. });