-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support live option for observable objects #540
Comments
Working fiddle http://jsfiddle.net/6wp03awL/ |
Sorry, my work around was a bit confusing there. The actual property is not an array in this case. e.g. the model I want is something like: http://jsfiddle.net/efpoa2Lg/1/ ko.validation.init({messagesOnModified: false, decorateInputElement: true, decorateElementOnModified: false}, true);
function AddressViewModel()
{
this.line1 = ko.observable().extend({required: true});
this.line2 = ko.observable().extend({required: true});
}
function ViewModel() {
this.email = ko.observable().extend({required: true, email: true});
this.address = ko.observable();
this.addAddress = function() {
this.address(new AddressViewModel());
}.bind(this);
this.removeAddress = function() {
this.address(null);
}.bind(this);
}
ko.applyBindings(ko.validatedObservable(new ViewModel(), {deep: true, observable: true, live: true})); Because address is not an array, validatedObservable only ever adds the first object to its group, even if that object's instance later changes. Using an array that only ever contains 0 or 1 items provides a work around for the behaviour I want, and a computed observable then presents that array as if it were a single object. |
The problem is the group is not updated when any of the properties on the ViewModel changes. There are some solutions for this issue:
I've updated your fiddle to use the second approach since the first solution is pretty straightforward http://jsfiddle.net/vd2o9q1a/ |
Could be a work around for now, but I don't really like relying on private methods like that. As a quick test I removed the Though I was wondering about handling of deep object graphs as a whole. For instance I wanted to have references back to the parent object but not follow those references with I was thinking of adding So then you could have: this.address = ko.observable().extend({validatable: {deep: true, live: true}});
this.selectedItem = ko.observable().extend({validatable: {deep: false}}); The This wouldn't be a breaking change as when |
The internal method is just a way to update the group and it was added to help |
I can't see any way to avoid individual grouping options though if you want The only alternative is to use My thought was you would only need to specify the grouping options on the individual observables when they're different from the default options to allow opt-in/opt-out. To reduce the typing the syntax could be I've assumed there is a reason While calling |
@chilversc Changing the behaviour of |
That was what I was thinking, but unlike #224 which only allows ignoring specific observables I thought of adding the same live/deep options as extenders. The logic would then be; check if the observable has an explicit live/deep setting if so use the explicit value (even if live is applied to a normal observable rather than an array). Otherwise use the live/deep setting from the options as per the normal rules (i.e. live from options only applies to arrays). This would allow setting the deep/live options on a per observable basis. function Nested() {
this.value = ko.observable().extend({number: true});
}
function Model() {
// deep from options, live from options only applies to arrays
this.objectDeepNotLive = ko.observable(new Nested());
// deep from options, explicit override for live
this.objectDeepAndLive = ko.observable(new Nested()).extend({live: true});
// explicit override for deep, live from options only applies to arrays
this.objectNotDeepNotLive = ko.observable(new Nested()).extend({deep: false});
// deep from options, live from options
this.arrayDeepAndLive = ko.observableArray();
// explicit override for deep and live
this.arrayNotDeepNotLive = ko.observableArray().extend({live: false, deep: false});
// ignored completely (as per #224), live and deep options are irrelevant
this.ignored = ko.observable().extend({ignoreInValidationGroup: true});
return ko.validatedObservable(this, {deep: true, live: true});
} Equally you could specify deep/true as false in the options and explicit override them on specific observables to true. Another consideration here would be to add |
I have some properties that contain 0 or 1 items that either start as null or can change their instance in the future. These properties are not included in the validation even when live is true.
Currently I'm using the following as a work around:
The text was updated successfully, but these errors were encountered: