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

https://github.com/Bowerbird/bowerbird-web · JavaScript · 158 lines · 119 code · 27 blank · 12 comment · 14 complexity · 35ec53ffa0b419df8e75185183645148 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. // AccountRegisterView
  8. // -------------------
  9. define(['jquery', 'underscore', 'backbone', 'ich', 'app', 'noext!/i18n'], function ($, _, Backbone, ich, app, i18n) {
  10. var AccountRegisterView = Backbone.Marionette.Layout.extend({
  11. viewType: 'form',
  12. className: 'register single',
  13. template: 'AccountRegister',
  14. events: {
  15. 'click #register': '_register',
  16. 'change #Name': '_contentChanged',
  17. 'change #Email': '_contentChanged',
  18. 'change #Password': '_contentChanged',
  19. 'click a': 'showItem'
  20. },
  21. initialize: function () {
  22. },
  23. serializeData: function () {
  24. return {
  25. Model: {
  26. AccountRegister: this.model.toJSON()
  27. }
  28. };
  29. },
  30. onShow: function () {
  31. this._showDetails();
  32. },
  33. showBootstrappedDetails: function () {
  34. this.initializeRegions();
  35. this._showDetails();
  36. },
  37. _showDetails: function () {
  38. },
  39. showItem: function (e) {
  40. e.preventDefault();
  41. Backbone.history.navigate($(e.currentTarget).attr('href'), { trigger: true });
  42. return false;
  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').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('#Name, #Email, #Password').removeClass('input-validation-error');
  94. if (_.any(errors, function (item) { return item.Field === 'Name'; })) {
  95. this.$el.find('#Name').addClass('input-validation-error');
  96. }
  97. if (_.any(errors, function (item) { return item.Field === 'Email' || item.Field === 'EmailInvalid'; })) {
  98. this.$el.find('#Email').addClass('input-validation-error');
  99. }
  100. if (_.any(errors, function (item) { return item.Field === 'Password'; })) {
  101. this.$el.find('#Password').addClass('input-validation-error');
  102. }
  103. },
  104. _contentChanged: function (e) {
  105. var target = $(e.currentTarget);
  106. var data = {};
  107. data[target.attr('id')] = target.attr('value');
  108. this.model.set(data);
  109. },
  110. _register: function (e) {
  111. e.preventDefault();
  112. this.$el.find('#register').attr('disabled', 'disabled').val('Joining...');
  113. var that = this;
  114. this.model.save(null, {
  115. success: function (model, response, options) {
  116. that.$el.find('#register').attr('disabled', 'disabled').val('Joined');
  117. that.onValidation(that.model, []);
  118. window.location.replace('/');
  119. },
  120. error: function (model, xhr, options) {
  121. that.$el.find('#register').removeAttr('disabled').val('Join');
  122. var data = JSON.parse(xhr.responseText);
  123. that.onValidation(that.model, data.Model.Errors);
  124. $('html, body').animate({ scrollTop: 0 }, 'slow');
  125. }
  126. });
  127. return false;
  128. }
  129. });
  130. return AccountRegisterView;
  131. });