Commits

Matthew Schinckel committed d9bad77

Use UMD definition.

Comments (0)

Files changed (1)

src/knockout.validators.js

-define(['jquery', 'knockout'], function($, ko){
-  
+(function (root, factory) {
+    if (typeof define === 'function' && define.amd) {
+        // AMD. Register as an anonymous module.
+        define(['jquery', 'knockout'], function ($, ko) {
+            // Also create a global in case some scripts
+            // that are loaded still are looking for
+            // a global even when an AMD loader is in use.
+            return factory($, ko);
+        });
+    } else {
+        factory(root.$, root.ko);
+    }
+}(this, function ($, ko) {
+    
   // We want to override the value bindingHandler, so we can inject html5 attributes.
   // We subscribe to the value's html5attrs attribute, so we can update automatically.
 
     init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
     var value = valueAccessor();
     var $element = $(element);
-    
-    var valueNeverChanged = ko.observable(true);
-    var monitor = value.subscribe(function() {
-      valueNeverChanged(false);
-      monitor.dispose();
-    });
-    
+        
     // Subscribe to the list of html5attrs we want to apply to this element.
     if (value.html5attrs) {
       var lastAttrs = value.html5attrs();
         }
       };
       value.errors.subscribe(validationMessage);
-      validationMessage(!valueNeverChanged() && value.errors() || []);
+      validationMessage(value.errors() || []);
     }
   };
   
         
   ko.subscribable.fn.validate = function(validators) {
     var validatorList = ko.observableArray([]);
-  
+    
+    // A method of not displaying validation errors until
+    // the value has been changed at least once.
+    // This still needs some thought, as we may need
+    // to validate when _any_ values in a model change...
+    var valueNeverChanged = ko.observable(true);
+    var monitor = this.subscribe(function() {
+      valueNeverChanged(false);
+      monitor.dispose();
+    });
+    
     var errors = ko.computed(function(){
+      if (valueNeverChanged()) {
+        return [];
+      }
       var value = ko.utils.unwrapObservable(this);
       return ko.utils.arrayFilter(validatorList(), function(condition){
           return condition.validator(value, condition.testValue);
   ko.subscribable.fn.validate.validationMessageTemplate = "<label class='help-block'></label>";
   
   return ko;
-});
+}));
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.