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

https://github.com/Bowerbird/bowerbird-web · JavaScript · 163 lines · 123 code · 28 blank · 12 comment · 13 complexity · 8caaef9cd107118a7d31d044a6bc203f 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. // AccountLoginView
  8. // ----------------
  9. define(['jquery', 'underscore', 'backbone', 'ich', 'app'], function ($, _, Backbone, ich, app) {
  10. var AccountLoginView = Backbone.Marionette.Layout.extend({
  11. viewType: 'form',
  12. className: 'login single',
  13. template: 'AccountLogin',
  14. events: {
  15. 'click #login': '_login',
  16. 'change #Email': '_contentChanged',
  17. 'change #Password': '_contentChanged',
  18. 'change #RememberMe': '_rememberMeChanged',
  19. 'click a': 'showItem'
  20. },
  21. initialize: function (options) {
  22. this.model.on('validated', this.onValidation, this);
  23. },
  24. serializeData: function () {
  25. return {
  26. Model: {
  27. AccountLogin: this.model.toJSON()
  28. }
  29. };
  30. },
  31. onShow: function () {
  32. this._showDetails();
  33. },
  34. showBootstrappedDetails: function () {
  35. this.initializeRegions();
  36. this._showDetails();
  37. },
  38. _showDetails: function () {
  39. },
  40. showItem: function (e) {
  41. e.preventDefault();
  42. Backbone.history.navigate($(e.currentTarget).attr('href'), { trigger: true });
  43. return false;
  44. },
  45. onValidation: function (obs, errors) {
  46. if (errors.length == 0) {
  47. this.$el.find('.validation-summary').slideUp(function () { $(this).remove(); });
  48. }
  49. if (errors.length > 0) {
  50. if (this.$el.find('.validation-summary').length == 0) {
  51. this.$el.find('.form').prepend(ich.ValidationSummary({
  52. SummaryMessage: 'Please correct the following before continuing:',
  53. Errors: errors,
  54. // 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
  55. Error: function () {
  56. return _.map(this.Messages, function (message) {
  57. return '<li class="validation-field-' + this.Field + '">' + message + '</li>';
  58. }, this).join('\n');
  59. }
  60. }));
  61. this.$el.find('.validation-summary').slideDown();
  62. } else {
  63. var that = this;
  64. // Remove items that are now valid
  65. this.$el.find('.validation-summary li').each(function () {
  66. var $li = that.$el.find(this);
  67. var found = _.find(errors, function (err) {
  68. return _.find(err.Messages, function (message) {
  69. return 'validation-field-' + err.Field === $li.attr('class') && message === $li.text();
  70. });
  71. });
  72. if (!found) {
  73. $li.slideUp(function () { $(this).remove(); });
  74. }
  75. });
  76. // Add items
  77. var lis = this.$el.find('.validation-summary li');
  78. _.each(errors, function (err) {
  79. _.each(err.Messages, function (message) {
  80. // Only add if the class and text is not found in li list
  81. var found = _.find(lis, function (li) {
  82. var $li = $(li);
  83. return $li.attr('class') === 'validation-field-' + err.Field && $li.text() === message;
  84. });
  85. if (!found) {
  86. var linew = $('<li class="validation-field-' + err.Field + '">' + message + '</li>').css({ display: 'none' });
  87. that.$el.find('.validation-summary ul').append(linew);
  88. linew.slideDown();
  89. }
  90. });
  91. }, this);
  92. }
  93. }
  94. this.$el.find('#Email, #Password').removeClass('input-validation-error');
  95. if (_.any(errors, function (item) { return item.Field === 'Email'; })) {
  96. this.$el.find('#Email').addClass('input-validation-error');
  97. }
  98. if (_.any(errors, function (item) { return item.Field === 'Password'; })) {
  99. this.$el.find('#Password').addClass('input-validation-error');
  100. }
  101. },
  102. _contentChanged: function (e) {
  103. var target = $(e.currentTarget);
  104. var data = {};
  105. data[target.attr('id')] = target.attr('value');
  106. this.model.set(data);
  107. },
  108. _rememberMeChanged: function (e) {
  109. this.model.set('RememberMe', e.currentTarget.checked);
  110. },
  111. _login: function (e) {
  112. e.preventDefault();
  113. this.$el.find('#login').attr('disabled', 'disabled').val('Authenticating...');
  114. var that = this;
  115. this.model.save(null, {
  116. success: function (model, response, options) {
  117. that.$el.find('#login').attr('disabled', 'disabled').val('Loading...');
  118. that.onValidation(that.model, []);
  119. if (that.model.get('ReturnUrl') === '') {
  120. that.model.set('ReturnUrl', '/');
  121. }
  122. window.location.replace(that.model.get('ReturnUrl'));
  123. },
  124. error: function (model, xhr, options) {
  125. that.$el.find('#login').removeAttr('disabled').val('Login');
  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 AccountLoginView;
  135. });