Editing Backbone.validateAll This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) To determine the performance benefits from using the Backbone.validateAll plugin to validate only the Model attributes that are set/saved instead of all Model attributes. Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script src='http://code.jquery.com/jquery.js'></script> <script src='http://underscorejs.org/underscore.js'></script> <script src='http://backbonejs.org/backbone.js'></script> <script src='http://gregfranko.com/javascripts/Backbone.validateAll.js'></script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) var User = Backbone.Model.extend({ // RegEx Patterns // ============== patterns: { specialCharacters: "[^a-zA-Z 0-9]+", digits: "[0-9]", email: "^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9.-]*[.]{1}[a-zA-Z]{2,6}$", phone: "^([0-9]{3})?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$" }, // Validators // ========== validators: { minLength: function(value, minLength) { return value.length >= minLength; }, maxLength: function(value, maxLength) { return value.length <= maxLength; }, pattern: function(value, pattern) { return new RegExp(pattern, "gi").test(value) ? true : false; }, isEmail: function(value) { return User.prototype.validators.pattern(value, User.prototype.patterns.email); }, isPhone: function(value) { return User.prototype.validators.pattern(value, User.prototype.patterns.phone); }, hasSpecialCharacter: function(value) { return User.prototype.validators.pattern(value, User.prototype.patterns.specialCharacters); }, hasDigit: function(value) { return User.prototype.validators.pattern(value, User.prototype.patterns.digits); } }, validate: function(attrs) { var errors = this.errors = {}; if(attrs.firstname != null) { if (!attrs.firstname) errors.firstname = 'firstname is required'; else if(!this.validators.minLength(attrs.firstname, 2)) errors.firstname = 'firstname is too short'; else if(!this.validators.maxLength(attrs.firstname, 15)) errors.firstname = 'firstname is too large'; else if(this.validators.hasSpecialCharacter(attrs.firstname)) errors.firstname = 'firstname cannot contain special characters'; } if(attrs.lastname != null) { if (!attrs.lastname) errors.lastname = 'lastname is required'; else if(!this.validators.minLength(attrs.lastname, 2)) errors.lastname = 'lastname is too short'; else if(!this.validators.maxLength(attrs.lastname, 15)) errors.lastname = 'lastname is too large'; else if(this.validators.hasSpecialCharacter(attrs.lastname)) errors.lastname = 'lastname cannot contain special characters'; } if(attrs.password != null) { if(!attrs.password) errors.password = 'password is required'; else if(!this.validators.minLength(attrs.password, 6)) errors.password = 'password is too short'; else if(!this.validators.maxLength(attrs.password, 15)) errors.password = 'password is too big'; else if(!this.validators.hasSpecialCharacter(attrs.password)) errors.password = 'password must contain a special character'; else if(!this.validators.hasDigit(attrs.password)) errors.password = 'password must contain a digit'; } if(attrs.email != null) { if (!attrs.email) errors.email = 'email is required'; else if(!this.validators.isEmail(attrs.email)) errors.email = 'email is not valid'; } if(attrs.phone != null) { if(!attrs.phone) errors.phone = 'phone is required'; else if(!this.validators.isPhone(attrs.phone)) errors.phone = 'phone number is invalid'; } if (!_.isEmpty(errors)) return errors; } }); var user = new User; Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code user.set({firstname: ""}); user.set({lastname: ""}); user.set({password: ""}); user.set({email: ""}); user.set({phone: ""}); Test 2 Title Async (check if this is an asynchronous test) Code user.set({firstname: ""}, {validateAll: false}); user.set({lastname: ""}, {validateAll: false}); user.set({password: ""}, {validateAll: false}); user.set({email: ""}, {validateAll: false}); user.set({phone: ""}, {validateAll: false});