PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/public/js/views/bank_account/index.js

https://github.com/bozz/cashflow
JavaScript | 78 lines | 64 code | 14 blank | 0 comment | 1 complexity | f26ba38b6e1cd768e8e7257649667f6b MD5 | raw file
  1. define(function(require) {
  2. var utils = require('utils');
  3. var tpl = require('text!/templates/bank_accounts/index.jst.ejs');
  4. var BankAccount = require('models/BankAccount');
  5. var BankAccountView = require('views/bank_account/detail');
  6. var BankAccounts = require('collections/BankAccounts');
  7. var BankAccountListView = Backbone.View.extend({
  8. template: _.template(tpl),
  9. events: {
  10. 'click button.btn-new': 'newAccount',
  11. 'click a.edit-link': 'editAccount',
  12. 'click button.btn-delete': 'deleteAccount'
  13. },
  14. initialize: function() {
  15. this.collection = BankAccounts.getInstance();
  16. this.collection.fetch();
  17. this.collection.on('reset', this.render, this);
  18. this.collection.on('add', this.render, this);
  19. this.collection.on('change', this.render, this);
  20. this.collection.on('destroy', this.render, this);
  21. },
  22. close: function() {
  23. this.remove();
  24. this.unbind();
  25. this.collection.off('reset', this.render);
  26. this.collection.off('add', this.render);
  27. this.collection.off('change', this.render);
  28. this.collection.off('destroy', this.render);
  29. },
  30. render: function() {
  31. this.$el.html(this.template({accounts: this.collection}));
  32. return this;
  33. },
  34. newAccount: function(event) {
  35. var model = new BankAccount();
  36. this.showAccountDetail(model);
  37. },
  38. editAccount: function(event) {
  39. var idSegs = $(event.currentTarget).attr('href').split('/');
  40. var id = _(idSegs).last();
  41. var model = this.collection.get(id);
  42. this.showAccountDetail(model);
  43. },
  44. showAccountDetail: function(model) {
  45. event.preventDefault();
  46. view = new BankAccountView({model: model, el: this.el});
  47. view.render();
  48. },
  49. deleteAccount: function(event) {
  50. var id = $(event.currentTarget).attr('value');
  51. var model = this.collection.get(id);
  52. bootbox.confirm("Do you really want to delete this bank account?", "Cancel", "Confirm Delete", function(result) {
  53. if (result) {
  54. model.destroy({
  55. success: function(model, response) {
  56. utils.alertSuccess("Bank account deleted");
  57. }
  58. });
  59. }
  60. });
  61. }
  62. });
  63. return BankAccountListView;
  64. });