Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var SignUpForm = Backbone.View.extend({
- events: {
- 'click #signUpButton': function (e) {
- e.preventDefault();
- this.signUp();
- }
- },
- initialize: function () {
- // This hooks up the validation
- // See: http://thedersen.com/projects/backbone-validation/#using-form-model-validation/validation-binding
- Backbone.Validation.bind(this);
- },
- signUp: function () {
- var data = this.$el.serializeObject();
- this.model.set(data);
- var modelValid = this.model.isValid(true);
- console.log('model valid', modelValid);
- var modelValidUsername = this.model.username.isValid(true);
- console.log('model username valid', modelValidUsername);
- var modelValidEmail = this.model.email.isValid(true);
- console.log('model email valid', modelValidEmail);
- // Check if the model is valid before saving
- // See: http://thedersen.com/projects/backbone-validation/#methods/isvalid
- if(this.model.isValid(true)){
- // this.model.save();
- alert('Great Success!');
- }
- },
- remove: function() {
- // Remove the validation binding
- // See: http://thedersen.com/projects/backbone-validation/#using-form-model-validation/unbinding
- Backbone.Validation.unbind(this);
- return Backbone.View.prototype.remove.apply(this, arguments);
- }
- });
- var SignUpModel = Backbone.Model.extend({
- defaults: {
- terms: false,
- gender: ''
- },
- validation: {
- username: {
- required: true,
- minLength: 3
- },
- email: {
- required: true,
- pattern: 'email'
- },
- password: {
- minLength: 4
- },
- repeatPassword: {
- equalTo: 'password',
- msg: 'The passwords does not match'
- }
- }
- });
- // Extend the callbacks to work with Bootstrap, as used in this example
- // See: http://thedersen.com/projects/backbone-validation/#configuration/callbacks
- _.extend(Backbone.Validation.callbacks, {
- valid: function (view, attr, selector) {
- var $el = view.$('[name=' + attr + ']'),
- $group = $el.closest('.form-group');
- $group.removeClass('has-error');
- $group.find('.help-block').html('').addClass('hidden');
- },
- invalid: function (view, attr, error, selector) {
- var $el = view.$('[name=' + attr + ']'),
- $group = $el.closest('.form-group');
- $group.addClass('has-error');
- $group.find('.help-block').html(error).removeClass('hidden');
- }
- });
- // https://github.com/hongymagic/jQuery.serializeObject
- $.fn.serializeObject = function () {
- "use strict";
- var a = {}, b = function (b, c) {
- var d = a[c.name];
- "undefined" != typeof d && d !== null ? $.isArray(d) ? d.push(c.value) : a[c.name] = [d, c.value] : a[c.name] = c.value
- };
- return $.each(this.serializeArray(), b), a
- };
- $(function () {
- var view = new SignUpForm({
- el: 'form',
- model: new SignUpModel()
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement