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

https://github.com/Bowerbird/bowerbird-web · JavaScript · 165 lines · 122 code · 30 blank · 13 comment · 14 complexity · 6b83226bd3c3ef4627ed3401bde1d56d 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. // AccountRequestPasswordUpdateFormView
  8. // ------------------------------------
  9. define(['jquery', 'underscore', 'backbone', 'app', 'ich'],
  10. function ($, _, Backbone, app, ich) {
  11. var AccountRequestPasswordUpdateFormView = Backbone.Marionette.Layout.extend({
  12. viewType: 'form',
  13. className: 'request-password-update single',
  14. template: 'AccountRequestPasswordUpdateForm',
  15. events: {
  16. 'click #cancel': '_cancel',
  17. 'click #save': '_save',
  18. 'change input#Email': '_contentChanged'
  19. },
  20. initialize: function (options) {
  21. },
  22. serializeData: function () {
  23. return {
  24. Model: {
  25. AccountRequestPasswordUpdate: {}
  26. }
  27. };
  28. },
  29. onShow: function () {
  30. this._showDetails();
  31. },
  32. showBootstrappedDetails: function () {
  33. this.initializeRegions();
  34. this._showDetails();
  35. },
  36. _showDetails: function () {
  37. },
  38. _contentChanged: function (e) {
  39. var target = $(e.currentTarget);
  40. var data = {};
  41. data[target.attr('id')] = target.attr('value');
  42. this.model.set(data);
  43. },
  44. onValidation: function (obs, errors) {
  45. if (errors.length == 0) {
  46. this.$el.find('.validation-summary').slideUp(function () { $(this).remove(); });
  47. }
  48. if (errors.length > 0) {
  49. if (this.$el.find('.validation-summary').length == 0) {
  50. this.$el.find('.form-details').prepend(ich.ValidationSummary({
  51. SummaryMessage: 'Please correct the following before continuing:',
  52. Errors: errors,
  53. // 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
  54. Error: function () {
  55. return _.map(this.Messages, function (message) {
  56. return '<li class="validation-field-' + this.Field + '">' + message + '</li>';
  57. }, this).join('\n');
  58. }
  59. }));
  60. this.$el.find('.validation-summary').slideDown();
  61. } else {
  62. var that = this;
  63. // Remove items that are now valid
  64. this.$el.find('.validation-summary li').each(function () {
  65. var $li = that.$el.find(this);
  66. var found = _.find(errors, function (err) {
  67. return _.find(err.Messages, function (message) {
  68. return 'validation-field-' + err.Field === $li.attr('class') && message === $li.text();
  69. });
  70. });
  71. if (!found) {
  72. $li.slideUp(function () { $(this).remove(); });
  73. }
  74. });
  75. // Add items
  76. var lis = this.$el.find('.validation-summary li');
  77. _.each(errors, function (err) {
  78. _.each(err.Messages, function (message) {
  79. // Only add if the class and text is not found in li list
  80. var found = _.find(lis, function (li) {
  81. var $li = $(li);
  82. return $li.attr('class') === 'validation-field-' + err.Field && $li.text() === message;
  83. });
  84. if (!found) {
  85. var linew = $('<li class="validation-field-' + err.Field + '">' + message + '</li>').css({ display: 'none' });
  86. that.$el.find('.validation-summary ul').append(linew);
  87. linew.slideDown();
  88. }
  89. });
  90. }, this);
  91. }
  92. }
  93. this.$el.find('#Email').removeClass('input-validation-error');
  94. if (_.any(errors, function (item) { return item.Field === 'Email' || item.Field === 'EmailInvalid'; })) {
  95. this.$el.find('#Email').addClass('input-validation-error');
  96. }
  97. },
  98. showSuccess: function () {
  99. if (this.$el.find('.validation-summary').length == 0) {
  100. this.$el.find('.validation-summary').slideUp(function () { $(this).remove(); });
  101. }
  102. this.$el.find('.form-details').prepend(ich.ValidationSummary({
  103. SummaryMessage: 'An email has been sent to you. Please follow the instructions in the email to reset your password.',
  104. Errors: []
  105. }));
  106. this.$el.find('.validation-summary').addClass('success').slideDown();
  107. },
  108. _cancel: function (e) {
  109. e.preventDefault();
  110. app.showPreviousContentView();
  111. return false;
  112. },
  113. _save: function (e) {
  114. e.preventDefault();
  115. this.$el.find('#save').attr('disabled', 'disabled').val('Searching...');
  116. var that = this;
  117. this.model.save(null, {
  118. success: function (model, response, options) {
  119. that.$el.find('#save').attr('disabled', 'disabled').val('Emailed');
  120. that.onValidation(that.model, []);
  121. that.showSuccess();
  122. //app.showPreviousContentView();
  123. },
  124. error: function (model, xhr, options) {
  125. that.$el.find('#save').removeAttr('disabled').val('Email');
  126. var data = JSON.parse(xhr.responseText);
  127. that.onValidation(that.model, data.Model.Errors);
  128. $('html, body').animate({ scrollTop: 0 }, 'slow');
  129. }
  130. });
  131. return false;
  132. }
  133. });
  134. return AccountRequestPasswordUpdateFormView;
  135. });