Skip to content

Commit

Permalink
Merge pull request #56 from racker/expose_addValidateToViewModel_as_a…
Browse files Browse the repository at this point in the history
…n_util

Expose addValidateToViewModel so we can validate view models without the need to bind it on the page
  • Loading branch information
bltavares committed Feb 7, 2014
2 parents 0ec3fe4 + a83da55 commit eadf0be
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 40 deletions.
58 changes: 41 additions & 17 deletions spec/view-model-spec.js
Expand Up @@ -2,33 +2,57 @@ describe('View Model with validatable observables', function () {
var viewModel;

beforeEach(function () {
var html;

viewModel = {
'name': ko.observable().extend({ 'required': ['Name is required.'] }),
'age': ko.observable().extend({ 'required': ['Age is required.'] })
};
});

html = '<div id="parent">' +
'<input id="name" data-bind="value: name"/>' +
'<input id="age" data-bind="value: age"/>' +
'</div>';
describe('when view model is rendered', function () {
beforeEach(function () {
var html;

setFixtures(html);
ko.applyBindings(viewModel, $('#parent')[0]);
});
html = '<div id="parent">' +
'<input id="name" data-bind="value: name"/>' +
'<input id="age" data-bind="value: age"/>' +
'</div>';

setFixtures(html);
ko.applyBindings(viewModel, $('#parent')[0]);
});

it('has a "validate" method added to it', function () {
expect(typeof viewModel.validate).toBe('function');
});

it('has a "validate" method added to it', function () {
expect(typeof viewModel.validate).toBe('function');
it('validates all its observables when "validate" is called', function () {
viewModel.validate();

expect(viewModel.name.isValid()).toBe(false);
expect(viewModel.name.validationMessage()).toBe('Name is required.');

expect(viewModel.age.isValid()).toBe(false);
expect(viewModel.age.validationMessage()).toBe('Age is required.');
});
});

it('validates all its observables when "validate" is called', function () {
viewModel.validate();
describe('when view model get validation added to it', function () {
beforeEach(function () {
ko.validation.utils.addValidateToViewModel(viewModel);
});

it('has a "validate" method added to it', function () {
expect(typeof viewModel.validate).toBe('function');
});

it('validates all its observables when "validate" is called', function () {
viewModel.validate();

expect(viewModel.name.isValid()).toBe(false);
expect(viewModel.name.validationMessage()).toBe('Name is required.');
expect(viewModel.name.isValid()).toBe(false);
expect(viewModel.name.validationMessage()).toBe('Name is required.');

expect(viewModel.age.isValid()).toBe(false);
expect(viewModel.age.validationMessage()).toBe('Age is required.');
expect(viewModel.age.isValid()).toBe(false);
expect(viewModel.age.validationMessage()).toBe('Age is required.');
});
});
});
46 changes: 23 additions & 23 deletions src/ko-validation.js
Expand Up @@ -59,6 +59,28 @@ ko.validation.utils = (function () {
}
};

self.addValidateToViewModel = function (viewModel) {
if (!viewModel) {
return;
}
viewModel.validate = viewModel.validate || function () {
var propertyName, observable, valid;

valid = true;
for (propertyName in viewModel) {
if (viewModel.hasOwnProperty(propertyName)) {
observable = viewModel[propertyName];
if (self.hasValidators(observable)) {
self.runValidations(observable);
valid = observable.isValid() && valid;
}
}
}

return valid;
};
};

return self;
}());

Expand Down Expand Up @@ -104,28 +126,6 @@ ko.validation.registerValidator = function (name, validatorFactory) {
return validationElement;
}

function addValidateToViewModel(viewModel) {
if (!viewModel) {
return;
}
viewModel.validate = viewModel.validate || function () {
var propertyName, observable, valid;

valid = true;
for (propertyName in viewModel) {
if (viewModel.hasOwnProperty(propertyName)) {
observable = viewModel[propertyName];
if (ko.validation.utils.hasValidators(observable)) {
ko.validation.utils.runValidations(observable);
valid = observable.isValid() && valid;
}
}
}

return valid;
};
}

function bindEventListenerToRunValidation(element, observableToValidate) {
var elementType, eventName;

Expand Down Expand Up @@ -224,7 +224,7 @@ ko.validation.registerValidator = function (name, validatorFactory) {
});
}

addValidateToViewModel(viewModel);
ko.validation.utils.addValidateToViewModel(viewModel);
}

return originalReturn;
Expand Down

0 comments on commit eadf0be

Please sign in to comment.