diff --git a/docs/backbone-validation.html b/docs/backbone-validation.html index 61d0a5a6..cdc43872 100644 --- a/docs/backbone-validation.html +++ b/docs/backbone-validation.html @@ -1,4 +1,4 @@ - backbone-validation.js

backbone-validation.js

Backbone.Validation v0.7.0

+ backbone-validation.js

backbone-validation.js

Backbone.Validation v0.7.1

Copyright (c) 2011-2012 Thomas Pedersen Distributed under MIT License

@@ -6,24 +6,23 @@

Documentation and full license available at: http://thedersen.com/projects/backbone-validation

Backbone.Validation = (function(_){
   'use strict';

Default options

  var defaultOptions = {
-      forceUpdate: false,
-      selector: 'name',
-      labelFormatter: 'sentenceCase',
-      valid: Function.prototype,
-      invalid: Function.prototype
-  };

Helper functions

  

Formatting functions used for formatting error messages

  var formatFunctions = {

Uses the configured label formatter to format the attribute name -to make it more readable for the user

      formatLabel: function(attrName, model) {
-        return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model);
-      },

Replaces nummeric placeholders like {0} in a string with arguments -passed to the function

      format: function() {
-        var args = Array.prototype.slice.call(arguments);
-        var text = args.shift();
-        return text.replace(/\{(\d+)\}/g, function(match, number) {
-          return typeof args[number] !== 'undefined' ? args[number] : match;
-        });
-      }
-  };
-  

Flattens an object + forceUpdate: false, + selector: 'name', + labelFormatter: 'sentenceCase', + valid: Function.prototype, + invalid: Function.prototype + };

Helper functions

Formatting functions used for formatting error messages

  var formatFunctions = {

Uses the configured label formatter to format the attribute name +to make it more readable for the user

    formatLabel: function(attrName, model) {
+      return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model);
+    },

Replaces nummeric placeholders like {0} in a string with arguments +passed to the function

    format: function() {
+      var args = Array.prototype.slice.call(arguments),
+          text = args.shift();
+      return text.replace(/\{(\d+)\}/g, function(match, number) {
+        return typeof args[number] !== 'undefined' ? args[number] : match;
+      });
+    }
+  };

Flattens an object eg:

var o = {
@@ -89,7 +88,7 @@
 the first error message is returned.
 If the attribute is valid, an empty string is returned.

    var validateAttr = function(model, attr, value, computed) {

Reduces the array of validators to an error message by applying all the validators and returning the first error -message, if any.

      return _.reduce(getValidators(model, attr), function(memo, validator){

Pass the format functions plus the default +message, if any.

      return _.reduce(getValidators(model, attr), function(memo, validator){

Pass the format functions plus the default validators as the context to the validator

        var ctx = _.extend({}, formatFunctions, defaultValidators),
             result = validator.fn.call(ctx, value, attr, validator.val, model, computed);
 
@@ -135,12 +134,9 @@
             return !validateAttr(this, option, flattened[option], _.extend({}, this.attributes));
           }
           if(_.isArray(option)){
-            for (var i = 0; i < option.length; i++) {
-              if(validateAttr(this, option[i], flattened[option[i]], _.extend({}, this.attributes))){
-                return false;
-              }
-            }
-            return true;
+            return _.reduce(option, function(memo, attr) {
+              return memo && !validateAttr(this, attr, flattened[attr], _.extend({}, this.attributes));
+            }, true, this);
           }
           if(option === true) {
             this.validate();
@@ -155,16 +151,17 @@
               validatedAttrs = getValidatedAttrs(model),
               allAttrs = _.extend({}, validatedAttrs, model.attributes, attrs),
               changedAttrs = flatten(attrs || allAttrs),
+
               result = validateModel(model, allAttrs);
 
           model._isValid = result.isValid;

After validation is performed, loop through all changed attributes -and call the valid callbacks so the view is updated.

          _.each(_.keys(validatedAttrs), function(attr){
+and call the valid callbacks so the view is updated.

          _.each(validatedAttrs, function(val, attr){
             var invalid = result.invalidAttrs.hasOwnProperty(attr);
             if(!invalid){
               opt.valid(view, attr, opt.selector);
             }
           });

After validation is performed, loop through all changed attributes -and call the invalid callback so the view is updated.

          _.each(_.keys(validatedAttrs), function(attr){
+and call the invalid callback so the view is updated.

          _.each(validatedAttrs, function(val, attr){
             var invalid = result.invalidAttrs.hasOwnProperty(attr),
                 changed = changedAttrs.hasOwnProperty(attr);
 
@@ -195,12 +192,13 @@
     };

Remove validation from a model whenever a model is removed from a collection

    var collectionRemove = function(model) {
       unbindModel(model);
-    };

Returns the public methods on Backbone.Validation

    return {

Current version of the library

      version: '0.7.0',

Called to configure the default options

      configure: function(options) {
+    };

Returns the public methods on Backbone.Validation

    return {

Current version of the library

      version: '0.7.1',

Called to configure the default options

      configure: function(options) {
         _.extend(defaultOptions, options);
       },

Hooks up validation on a view with a model or collection

      bind: function(view, options) {
         var model = view.model,
             collection = view.collection;
+
         options = _.extend({}, defaultOptions, defaultCallbacks, options);
 
         if(typeof model === 'undefined' && typeof collection === 'undefined'){
@@ -211,7 +209,7 @@
         if(model) {
           bindModel(view, model, options);
         }
-        if(collection) {
+        else if(collection) {
           collection.each(function(model){
             bindModel(view, model, options);
           });
@@ -239,13 +237,13 @@
   }());

Callbacks

  var defaultCallbacks = Validation.callbacks = {

Gets called when a previously invalid field in the view becomes valid. Removes any error message. Should be overridden with custom functionality.

    valid: function(view, attr, selector) {
-      view.$('[' + selector + '~=' + attr + ']')
+      view.$('[' + selector + '~="' + attr + '"]')
           .removeClass('invalid')
           .removeAttr('data-error');
     },

Gets called when a field in the view becomes invalid. Adds a error message. Should be overridden with custom functionality.

    invalid: function(view, attr, error, selector) {
-      view.$('[' + selector + '~=' + attr + ']')
+      view.$('[' + selector + '~="' + attr + '"]')
           .addClass('invalid')
           .attr('data-error', error);
     }
@@ -295,15 +293,15 @@
       return (model.labels && model.labels[attrName]) || defaultLabelFormatters.sentenceCase(attrName, model);
     }
   };

Built in validators

  var defaultValidators = Validation.validators = (function(){

Use native trim when defined

    var trim = String.prototype.trim ?
-        function(text) {
-            return text === null ? '' : String.prototype.trim.call(text);
-        } :
-        function(text) {
-            var trimLeft = /^\s+/,
-                trimRight = /\s+$/;
+      function(text) {
+        return text === null ? '' : String.prototype.trim.call(text);
+      } :
+      function(text) {
+        var trimLeft = /^\s+/,
+            trimRight = /\s+$/;
 
-            return text === null ? '' : text.toString().replace(trimLeft, '').replace(trimRight, '');
-        };

Determines whether or not a value is a number

    var isNumber = function(value){
+        return text === null ? '' : text.toString().replace(trimLeft, '').replace(trimRight, '');
+      };

Determines whether or not a value is a number

    var isNumber = function(value){
       return _.isNumber(value) || (_.isString(value) && value.match(defaultPatterns.number));
     };

Determines whether or not not a value is empty

    var hasValue = function(value) {
       return !(_.isNull(value) || _.isUndefined(value) || (_.isString(value) && trim(value) === ''));