/Backup/Src/Bowerbird.Website/js/bowerbird/views/accountUpdatePasswordFormView.js

https://github.com/Bowerbird/bowerbird-web · JavaScript · 155 lines · 114 code · 27 blank · 14 comment · 12 complexity · d04b1b56d64c519128366f144dfecb1d MD5 · raw file

  1. /// <reference path="../../libs/log.js" />
  2. /// <reference path="../../libs/require/require.js" />
  3. /// <reference path="../../libs/jquery/jquery-1.7.2.js" />
  4. /// <reference path="../../libs/underscore/underscore.js" />
  5. /// <reference path="../../libs/backbone/backbone.js" />
  6. /// <reference path="../../libs/backbone.marionette/backbone.marionette.js" />
  7. // AccountUpdatePasswordFormView
  8. // -----------------------------
  9. define(['jquery', 'underscore', 'backbone', 'app', 'ich'],
  10. function ($, _, Backbone, app, ich) {
  11. var AccountUpdatePasswordFormView = Backbone.Marionette.Layout.extend({
  12. viewType: 'form',
  13. className: 'update-password single',
  14. template: 'AccountUpdatePasswordForm',
  15. events: {
  16. 'click #cancel': '_cancel',
  17. 'click #save': '_save',
  18. 'change input#NewPassword': '_contentChanged'
  19. },
  20. initialize: function (options) {
  21. this.errors = options.errors;
  22. },
  23. serializeData: function () {
  24. return {
  25. Model: {}
  26. };
  27. },
  28. onShow: function () {
  29. this._showDetails();
  30. },
  31. showBootstrappedDetails: function () {
  32. this.initializeRegions();
  33. this._showDetails();
  34. },
  35. _showDetails: function () {
  36. // In the case the user has come to this page with an invalid key that has been bootstrapped in, we immediately
  37. // show the invalid key error
  38. if (this.errors) {
  39. this.onValidation(this.model, this.errors);
  40. }
  41. },
  42. _contentChanged: function (e) {
  43. var target = $(e.currentTarget);
  44. var data = {};
  45. data[target.attr('id')] = target.attr('value');
  46. this.model.set(data);
  47. },
  48. onValidation: function (obs, errors) {
  49. if (errors.length == 0) {
  50. this.$el.find('.validation-summary').slideUp(function () { $(this).remove(); });
  51. }
  52. if (errors.length > 0) {
  53. if (this.$el.find('.validation-summary').length == 0) {
  54. this.$el.find('.form-details').prepend(ich.ValidationSummary({
  55. SummaryMessage: 'Please correct the following before continuing:',
  56. Errors: errors,
  57. // Due to a bug in mustache.js where you can't reference a parent element in a string array loop, we have to build the HTML here
  58. Error: function () {
  59. return _.map(this.Messages, function (message) {
  60. return '<li class="validation-field-' + this.Field + '">' + message + '</li>';
  61. }, this).join('\n');
  62. }
  63. }));
  64. this.$el.find('.validation-summary').slideDown();
  65. } else {
  66. var that = this;
  67. // Remove items that are now valid
  68. this.$el.find('.validation-summary li').each(function () {
  69. var $li = that.$el.find(this);
  70. var found = _.find(errors, function (err) {
  71. return _.find(err.Messages, function (message) {
  72. return 'validation-field-' + err.Field === $li.attr('class') && message === $li.text();
  73. });
  74. });
  75. if (!found) {
  76. $li.slideUp(function () { $(this).remove(); });
  77. }
  78. });
  79. // Add items
  80. var lis = this.$el.find('.validation-summary li');
  81. _.each(errors, function (err) {
  82. _.each(err.Messages, function (message) {
  83. // Only add if the class and text is not found in li list
  84. var found = _.find(lis, function (li) {
  85. var $li = $(li);
  86. return $li.attr('class') === 'validation-field-' + err.Field && $li.text() === message;
  87. });
  88. if (!found) {
  89. var linew = $('<li class="validation-field-' + err.Field + '">' + message + '</li>').css({ display: 'none' });
  90. that.$el.find('.validation-summary ul').append(linew);
  91. linew.slideDown();
  92. }
  93. });
  94. }, this);
  95. }
  96. }
  97. this.$el.find('#NewPassword').removeClass('input-validation-error');
  98. if (_.any(errors, function (item) { return item.Field === 'NewPassword'; })) {
  99. this.$el.find('#NewPassword').addClass('input-validation-error');
  100. }
  101. },
  102. _cancel: function (e) {
  103. e.preventDefault();
  104. app.showPreviousContentView();
  105. return false;
  106. },
  107. _save: function (e) {
  108. e.preventDefault();
  109. this.$el.find('#save').attr('disabled', 'disabled').val('Saving...');
  110. var that = this;
  111. this.model.save(null, {
  112. success: function (model, response, options) {
  113. that.$el.find('#save').attr('disabled', 'disabled').val('Saved');
  114. that.onValidation(that.model, []);
  115. app.showPreviousContentView();
  116. },
  117. error: function (model, xhr, options) {
  118. that.$el.find('#save').removeAttr('disabled').val('Save');
  119. var data = JSON.parse(xhr.responseText);
  120. that.onValidation(that.model, data.Model.Errors);
  121. $('html, body').animate({ scrollTop: 0 }, 'slow');
  122. }
  123. });
  124. return false;
  125. }
  126. });
  127. return AccountUpdatePasswordFormView;
  128. });