0

I'm having trouble combining namespacing with knockout validation. This breaks the validation:

myNameSpace = {
    viewModel: {
        name: ko.observable().extend({ digit: { digit: true, message: "digits only"} })
    }
};
ko.validation.init({});
ko.applyBindings(myNameSpace);

​As opposed to:

myNameSpace = {
    viewModel: {
        name: ko.observable().extend({ digit: { digit: true, message: "digits only"} })
    }
};
ko.validation.init({});
ko.applyBindings(myNameSpace.viewModel);

Which works fine.

Can anyone explain to me the difference? ​ ​

1 Answers1

0

This is about the existence of objects and properties. You might have had your bindings to the "name" property. If you do ko.applyBindings(myNameSpace);, ko looks for a property "name" within myNameSpace object. This does not exist.

When you bind it with ko.applyBindings(myNameSpace.viewModel);, it looks for name within the view model object. It does find it and there fore is able to bind to the property or observable.

In the first case however, if you were to use the data-bind with "value : viewModel.name" it would work, because it would be looking for the name property of the viewModel property of the myNameSpace object.

Ravi Y
  • 4,296
  • 2
  • 27
  • 38