Advertisement
Guest User

Untitled

a guest
May 19th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. var SignUpForm = Backbone.View.extend({
  2. events: {
  3. 'click #signUpButton': function (e) {
  4. e.preventDefault();
  5. this.signUp();
  6. }
  7. },
  8.  
  9. initialize: function () {
  10. // This hooks up the validation
  11. // See: http://thedersen.com/projects/backbone-validation/#using-form-model-validation/validation-binding
  12. Backbone.Validation.bind(this);
  13. },
  14.  
  15. signUp: function () {
  16. var data = this.$el.serializeObject();
  17.  
  18. this.model.set(data);
  19.  
  20. var modelValid = this.model.isValid(true);
  21. console.log('model valid', modelValid);
  22.  
  23. var modelValidUsername = this.model.username.isValid(true);
  24. console.log('model username valid', modelValidUsername);
  25.  
  26. var modelValidEmail = this.model.email.isValid(true);
  27. console.log('model email valid', modelValidEmail);
  28.  
  29. // Check if the model is valid before saving
  30. // See: http://thedersen.com/projects/backbone-validation/#methods/isvalid
  31. if(this.model.isValid(true)){
  32. // this.model.save();
  33. alert('Great Success!');
  34. }
  35. },
  36.  
  37. remove: function() {
  38. // Remove the validation binding
  39. // See: http://thedersen.com/projects/backbone-validation/#using-form-model-validation/unbinding
  40. Backbone.Validation.unbind(this);
  41. return Backbone.View.prototype.remove.apply(this, arguments);
  42. }
  43. });
  44.  
  45. var SignUpModel = Backbone.Model.extend({
  46. defaults: {
  47. terms: false,
  48. gender: ''
  49. },
  50. validation: {
  51. username: {
  52. required: true,
  53. minLength: 3
  54. },
  55. email: {
  56. required: true,
  57. pattern: 'email'
  58. },
  59. password: {
  60. minLength: 4
  61. },
  62. repeatPassword: {
  63. equalTo: 'password',
  64. msg: 'The passwords does not match'
  65. }
  66. }
  67. });
  68.  
  69. // Extend the callbacks to work with Bootstrap, as used in this example
  70. // See: http://thedersen.com/projects/backbone-validation/#configuration/callbacks
  71. _.extend(Backbone.Validation.callbacks, {
  72. valid: function (view, attr, selector) {
  73. var $el = view.$('[name=' + attr + ']'),
  74. $group = $el.closest('.form-group');
  75.  
  76. $group.removeClass('has-error');
  77. $group.find('.help-block').html('').addClass('hidden');
  78. },
  79. invalid: function (view, attr, error, selector) {
  80. var $el = view.$('[name=' + attr + ']'),
  81. $group = $el.closest('.form-group');
  82.  
  83. $group.addClass('has-error');
  84. $group.find('.help-block').html(error).removeClass('hidden');
  85. }
  86. });
  87.  
  88.  
  89. // https://github.com/hongymagic/jQuery.serializeObject
  90. $.fn.serializeObject = function () {
  91. "use strict";
  92. var a = {}, b = function (b, c) {
  93. var d = a[c.name];
  94. "undefined" != typeof d && d !== null ? $.isArray(d) ? d.push(c.value) : a[c.name] = [d, c.value] : a[c.name] = c.value
  95. };
  96. return $.each(this.serializeArray(), b), a
  97. };
  98.  
  99. $(function () {
  100. var view = new SignUpForm({
  101. el: 'form',
  102. model: new SignUpModel()
  103. });
  104. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement