PageRenderTime 73ms CodeModel.GetById 44ms RepoModel.GetById 1ms app.codeStats 0ms

/public/js/views/bank_account/form.js

https://github.com/bozz/cashflow
JavaScript | 104 lines | 77 code | 18 blank | 9 comment | 14 complexity | c7f0b0c7064b6debbf0fcece0bc1632d MD5 | raw file
  1. define([
  2. 'text!/templates/bank_accounts/form.jst.ejs',
  3. 'models/BankAccount'
  4. ], function(tpl, BankAccount) {
  5. var BankAccountFormView = Backbone.View.extend({
  6. template: _.template(tpl),
  7. events: {
  8. 'click a.btn-submit': 'saveAccount'
  9. },
  10. initialize: function (options) {
  11. options = options || {};
  12. if(!options.id && !options.model) {
  13. this.model = new BankAccount();
  14. } else if(options.model) {
  15. this.model = options.model;
  16. } else {
  17. this.model = new BankAccount({
  18. id: options.id
  19. });
  20. this.model.on('change', this.render, this);
  21. this.model.fetch();
  22. }
  23. },
  24. close: function() {
  25. this.remove();
  26. this.unbind();
  27. },
  28. render: function() {
  29. var html = this.template({model: this.model, showFormActions: true});
  30. this.$el.html(html);
  31. return this;
  32. },
  33. saveAccount: function(event) {
  34. event.preventDefault();
  35. this.model.set({
  36. bank: $('#ba-bank').val(),
  37. name: $('#ba-name').val(),
  38. account_number: $('#ba-number').val(),
  39. // currency: $('#ba-currency').val(),
  40. // initial_cents: $('#ba-initial').val(),
  41. description: $('#ba-description').val()
  42. });
  43. var options = {
  44. wait: true,
  45. success: $.proxy(this.handleSuccess, this),
  46. error: $.proxy(this.handleError, this)
  47. };
  48. this.$el.mask("Saving...");
  49. if (this.model.isNew()) {
  50. // this.collection.create(this.model, options);
  51. } else {
  52. this.model.save(this.model, options);
  53. }
  54. },
  55. handleSuccess: function(event) {
  56. if(event.preventDefault) { event.preventDefault(); }
  57. this.$el.unmask();
  58. // utils.alertSuccess("Settings saved successfully");
  59. alert("refresh relevant areas of ui...");
  60. },
  61. handleError: function(model, response) {
  62. $(this.el).unmask();
  63. // if validation errors
  64. if(response.status == 422) {
  65. // reset any previous errors
  66. $('div.control-group').removeClass('error').find('p.error-msg').remove();
  67. // mark all found validation errors
  68. var errors = $.parseJSON(response.responseText);
  69. for(var key in errors) {
  70. $('[name=' + key + ']')
  71. .closest('div.control-group')
  72. .addClass('error')
  73. .find('div.controls')
  74. .append('<p class="help-block error-msg">' + errors[key] + '</p>');
  75. }
  76. // reset values in model since already saved locally
  77. if(!model.isNew()){
  78. model.fetch();
  79. }
  80. } else {
  81. // utils.alertError(response.errorMsg);
  82. }
  83. }
  84. });
  85. return BankAccountFormView;
  86. });