Skip to content

Commit

Permalink
Prevent early initialization
Browse files Browse the repository at this point in the history
Initialization will not occur unless an extender is applied to an
observable or applyBindings is called.

Closes #545
  • Loading branch information
crissdev committed Mar 28, 2015
1 parent 217398b commit 1f4c21a
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 121 deletions.
79 changes: 40 additions & 39 deletions dist/knockout.validation-with-locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ kv.configuration = configuration;

var isInitialized = 0,
configuration = kv.configuration,
utils = kv.utils;
utils = kv.utils,
bindingHandlersValidatable = false;

function cleanUpSubscriptions(context) {
forEach(context.subscriptions, function (subscription) {
Expand Down Expand Up @@ -315,11 +316,22 @@ kv.configuration = configuration;

extend(configuration, options);

if (!bindingHandlersValidatable) {
bindingHandlersValidatable = true;

kv.makeBindingHandlerValidatable('value');
kv.makeBindingHandlerValidatable('checked');
if (ko.bindingHandlers.textInput) {
kv.makeBindingHandlerValidatable('textInput');
}
kv.makeBindingHandlerValidatable('selectedOptions');
}

if (configuration.registerExtenders) {
kv.registerExtenders();
}

isInitialized = 1;
isInitialized = 1;
},

// resets the config back to its original state
Expand Down Expand Up @@ -998,64 +1010,47 @@ kv.rules['unique'] = {
},
message: 'Please make sure the value is unique.'
};


//now register all of these!
(function () {
kv.registerExtenders();
}());
;// The core binding handler
// this allows us to setup any value binding that internally always
// performs the same functionality
ko.bindingHandlers['validationCore'] = (function () {

return {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var config = kv.utils.getConfigOptions(element);
var observable = valueAccessor();

// parse html5 input validation attributes, optional feature
if (config.parseInputAttributes) {
kv.utils.async(function () { kv.parseInputValidationAttributes(element, valueAccessor); });
}
ko.bindingHandlers['validationCore'] = {
init: function (element, valueAccessor) {
var config = kv.utils.getConfigOptions(element);
var observable = valueAccessor();

// parse html5 input validation attributes, optional feature
if (config.parseInputAttributes) {
kv.utils.async(function () { kv.parseInputValidationAttributes(element, valueAccessor); });
}

if (kv.utils.isValidatable(observable)) {
// if requested insert message element and apply bindings
if (config.insertMessages && kv.utils.isValidatable(observable)) {
if (config.insertMessages) {

// insert the <span></span>
var validationMessageElement = kv.insertValidationMessage(element);

// if we're told to use a template, make sure that gets rendered
if (config.messageTemplate) {
ko.renderTemplate(config.messageTemplate, { field: observable }, null, validationMessageElement, 'replaceNode');
} else {
ko.applyBindingsToNode(validationMessageElement, { validationMessage: observable });
ko.renderTemplate(config.messageTemplate, {field: observable}, null, validationMessageElement, 'replaceNode');
}
else {
ko.applyBindingsToNode(validationMessageElement, {validationMessage: observable});
}
}

// write the html5 attributes if indicated by the config
if (config.writeInputAttributes && kv.utils.isValidatable(observable)) {

if (config.writeInputAttributes) {
kv.writeInputValidationAttributes(element, valueAccessor);
}

// if requested, add binding to decorate element
if (config.decorateInputElement && kv.utils.isValidatable(observable)) {
ko.applyBindingsToNode(element, { validationElement: observable });
if (config.decorateInputElement) {
ko.applyBindingsToNode(element, {validationElement: observable});
}
}
};

}());

// override for KO's default 'value', 'checked', 'textInput' and selectedOptions bindings
kv.makeBindingHandlerValidatable("value");
kv.makeBindingHandlerValidatable("checked");
if (ko.bindingHandlers.textInput) {
kv.makeBindingHandlerValidatable("textInput");
}
kv.makeBindingHandlerValidatable("selectedOptions");

}
};

ko.bindingHandlers['validationMessage'] = { // individual error message, if modified or post binding
update: function (element, valueAccessor) {
Expand Down Expand Up @@ -1478,6 +1473,12 @@ ko.applyBindings = function (viewModel, rootNode) {
origApplyBindings(viewModel, rootNode);
};

var origApplyExtenders = ko.subscribable.fn.extend;
ko.subscribable.fn.extend = function(requestedExtenders) {
kv.init();
return origApplyExtenders.call(this, requestedExtenders);
};

ko.validatedObservable = function (initialValue, options) {
if (!options && !kv.utils.isObject(initialValue)) {
return ko.observable(initialValue).extend({ validatable: true });
Expand Down
2 changes: 1 addition & 1 deletion dist/knockout.validation-with-locales.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/knockout.validation-with-locales.min.js.map

Large diffs are not rendered by default.

79 changes: 40 additions & 39 deletions dist/knockout.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ kv.configuration = configuration;

var isInitialized = 0,
configuration = kv.configuration,
utils = kv.utils;
utils = kv.utils,
bindingHandlersValidatable = false;

function cleanUpSubscriptions(context) {
forEach(context.subscriptions, function (subscription) {
Expand Down Expand Up @@ -315,11 +316,22 @@ kv.configuration = configuration;

extend(configuration, options);

if (!bindingHandlersValidatable) {
bindingHandlersValidatable = true;

kv.makeBindingHandlerValidatable('value');
kv.makeBindingHandlerValidatable('checked');
if (ko.bindingHandlers.textInput) {
kv.makeBindingHandlerValidatable('textInput');
}
kv.makeBindingHandlerValidatable('selectedOptions');
}

if (configuration.registerExtenders) {
kv.registerExtenders();
}

isInitialized = 1;
isInitialized = 1;
},

// resets the config back to its original state
Expand Down Expand Up @@ -998,64 +1010,47 @@ kv.rules['unique'] = {
},
message: 'Please make sure the value is unique.'
};


//now register all of these!
(function () {
kv.registerExtenders();
}());
;// The core binding handler
// this allows us to setup any value binding that internally always
// performs the same functionality
ko.bindingHandlers['validationCore'] = (function () {

return {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var config = kv.utils.getConfigOptions(element);
var observable = valueAccessor();

// parse html5 input validation attributes, optional feature
if (config.parseInputAttributes) {
kv.utils.async(function () { kv.parseInputValidationAttributes(element, valueAccessor); });
}
ko.bindingHandlers['validationCore'] = {
init: function (element, valueAccessor) {
var config = kv.utils.getConfigOptions(element);
var observable = valueAccessor();

// parse html5 input validation attributes, optional feature
if (config.parseInputAttributes) {
kv.utils.async(function () { kv.parseInputValidationAttributes(element, valueAccessor); });
}

if (kv.utils.isValidatable(observable)) {
// if requested insert message element and apply bindings
if (config.insertMessages && kv.utils.isValidatable(observable)) {
if (config.insertMessages) {

// insert the <span></span>
var validationMessageElement = kv.insertValidationMessage(element);

// if we're told to use a template, make sure that gets rendered
if (config.messageTemplate) {
ko.renderTemplate(config.messageTemplate, { field: observable }, null, validationMessageElement, 'replaceNode');
} else {
ko.applyBindingsToNode(validationMessageElement, { validationMessage: observable });
ko.renderTemplate(config.messageTemplate, {field: observable}, null, validationMessageElement, 'replaceNode');
}
else {
ko.applyBindingsToNode(validationMessageElement, {validationMessage: observable});
}
}

// write the html5 attributes if indicated by the config
if (config.writeInputAttributes && kv.utils.isValidatable(observable)) {

if (config.writeInputAttributes) {
kv.writeInputValidationAttributes(element, valueAccessor);
}

// if requested, add binding to decorate element
if (config.decorateInputElement && kv.utils.isValidatable(observable)) {
ko.applyBindingsToNode(element, { validationElement: observable });
if (config.decorateInputElement) {
ko.applyBindingsToNode(element, {validationElement: observable});
}
}
};

}());

// override for KO's default 'value', 'checked', 'textInput' and selectedOptions bindings
kv.makeBindingHandlerValidatable("value");
kv.makeBindingHandlerValidatable("checked");
if (ko.bindingHandlers.textInput) {
kv.makeBindingHandlerValidatable("textInput");
}
kv.makeBindingHandlerValidatable("selectedOptions");

}
};

ko.bindingHandlers['validationMessage'] = { // individual error message, if modified or post binding
update: function (element, valueAccessor) {
Expand Down Expand Up @@ -1478,6 +1473,12 @@ ko.applyBindings = function (viewModel, rootNode) {
origApplyBindings(viewModel, rootNode);
};

var origApplyExtenders = ko.subscribable.fn.extend;
ko.subscribable.fn.extend = function(requestedExtenders) {
kv.init();
return origApplyExtenders.call(this, requestedExtenders);
};

ko.validatedObservable = function (initialValue, options) {
if (!options && !kv.utils.isObject(initialValue)) {
return ko.observable(initialValue).extend({ validatable: true });
Expand Down
2 changes: 1 addition & 1 deletion dist/knockout.validation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/knockout.validation.min.js.map

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

var isInitialized = 0,
configuration = ko.validation.configuration,
utils = ko.validation.utils;
utils = ko.validation.utils,
bindingHandlersValidatable = false;

function cleanUpSubscriptions(context) {
ko.utils.arrayForEach(context.subscriptions, function (subscription) {
Expand Down Expand Up @@ -117,11 +118,22 @@

ko.utils.extend(configuration, options);

if (!bindingHandlersValidatable) {
bindingHandlersValidatable = true;

ko.validation.makeBindingHandlerValidatable('value');
ko.validation.makeBindingHandlerValidatable('checked');
if (ko.bindingHandlers.textInput) {
ko.validation.makeBindingHandlerValidatable('textInput');
}
ko.validation.makeBindingHandlerValidatable('selectedOptions');
}

if (configuration.registerExtenders) {
ko.validation.registerExtenders();
}

isInitialized = 1;
isInitialized = 1;
},

// resets the config back to its original state
Expand Down
51 changes: 20 additions & 31 deletions src/bindingHandlers.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,44 @@
// The core binding handler
// this allows us to setup any value binding that internally always
// performs the same functionality
ko.bindingHandlers['validationCore'] = (function () {

return {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var config = ko.validation.utils.getConfigOptions(element);
var observable = valueAccessor();

// parse html5 input validation attributes, optional feature
if (config.parseInputAttributes) {
ko.validation.utils.async(function () { ko.validation.parseInputValidationAttributes(element, valueAccessor); });
}
ko.bindingHandlers['validationCore'] = {
init: function (element, valueAccessor) {
var config = ko.validation.utils.getConfigOptions(element);
var observable = valueAccessor();

// parse html5 input validation attributes, optional feature
if (config.parseInputAttributes) {
ko.validation.utils.async(function () { ko.validation.parseInputValidationAttributes(element, valueAccessor); });
}

if (ko.validation.utils.isValidatable(observable)) {
// if requested insert message element and apply bindings
if (config.insertMessages && ko.validation.utils.isValidatable(observable)) {
if (config.insertMessages) {

// insert the <span></span>
var validationMessageElement = ko.validation.insertValidationMessage(element);

// if we're told to use a template, make sure that gets rendered
if (config.messageTemplate) {
ko.renderTemplate(config.messageTemplate, { field: observable }, null, validationMessageElement, 'replaceNode');
} else {
ko.applyBindingsToNode(validationMessageElement, { validationMessage: observable });
ko.renderTemplate(config.messageTemplate, {field: observable}, null, validationMessageElement, 'replaceNode');
}
else {
ko.applyBindingsToNode(validationMessageElement, {validationMessage: observable});
}
}

// write the html5 attributes if indicated by the config
if (config.writeInputAttributes && ko.validation.utils.isValidatable(observable)) {

if (config.writeInputAttributes) {
ko.validation.writeInputValidationAttributes(element, valueAccessor);
}

// if requested, add binding to decorate element
if (config.decorateInputElement && ko.validation.utils.isValidatable(observable)) {
ko.applyBindingsToNode(element, { validationElement: observable });
if (config.decorateInputElement) {
ko.applyBindingsToNode(element, {validationElement: observable});
}
}
};

}());

// override for KO's default 'value', 'checked', 'textInput' and selectedOptions bindings
ko.validation.makeBindingHandlerValidatable("value");
ko.validation.makeBindingHandlerValidatable("checked");
if (ko.bindingHandlers.textInput) {
ko.validation.makeBindingHandlerValidatable("textInput");
}
ko.validation.makeBindingHandlerValidatable("selectedOptions");

}
};

ko.bindingHandlers['validationMessage'] = { // individual error message, if modified or post binding
update: function (element, valueAccessor) {
Expand Down
6 changes: 6 additions & 0 deletions src/ko.extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ ko.applyBindings = function (viewModel, rootNode) {
origApplyBindings(viewModel, rootNode);
};

var origApplyExtenders = ko.subscribable.fn.extend;
ko.subscribable.fn.extend = function(requestedExtenders) {
ko.validation.init();
return origApplyExtenders.call(this, requestedExtenders);
};

ko.validatedObservable = function (initialValue, options) {
if (!options && !ko.validation.utils.isObject(initialValue)) {
return ko.observable(initialValue).extend({ validatable: true });
Expand Down
6 changes: 0 additions & 6 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,3 @@ ko.validation.rules['unique'] = {
},
message: 'Please make sure the value is unique.'
};


//now register all of these!
(function () {
ko.validation.registerExtenders();
}());

0 comments on commit 1f4c21a

Please sign in to comment.