This is a migrated thread and some comments may be shown as answers.

Dependent methods that depend on other dependent methods (with jsfiddle)

1 Answer 41 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 15 Oct 2012, 02:48 PM
I am looking to have a validation message only be populated when needed. So I have an observable object with 3 fields: Value, IsValid, and Message. IsValid is dependent on Value, and Message is dependent on IsValid.

http://jsfiddle.net/GExfL/
var viewModel = kendo.observable({
    OrderNumber: {
        Value: '',
        IsValid: function(){
            return (this.get('Value').length >= 8);
        },
        Message: function(){
            if(this.get('IsValid')){
                return '';
            }
            else{
                return 'Text length must be at least 8 characters.';
            }
        }
    }
});

It doesn't appear that this is allowed, but is there an easy way to design the VM to get something similar?

The closest I've come up with is a utility method that is shared by both IsValid and Message (http://jsfiddle.net/GExfL/1/), but it seems a little verbose.

1 Answer, 1 is accepted

Sort by
0
Martin
Top achievements
Rank 1
answered on 09 Nov 2012, 03:31 PM
Actually quite a simple solution: http://jsfiddle.net/GExfL/2/

var viewModel = kendo.observable({
    OrderNumber: {
        Value: '',
        IsValid: function(){
            return (this.get('Value').length >= 8);
        },
        Message: function(){
            if(this.IsValid()){
                return '';
            }
            else{
                return 'Text length must be at least 8 characters.';
            }
        }
    }
});
I believe that you only need to use this.get on raw values, not calculated values.
Tags
MVVM
Asked by
Paul
Top achievements
Rank 1
Answers by
Martin
Top achievements
Rank 1
Share this question
or