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

Is it possible to force refresh of the view?

1 Answer 265 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Boris
Top achievements
Rank 1
Boris asked on 20 Jun 2016, 02:01 PM

Hello,

I have an input element. It displays duration of an activity, so valid values are only numbers. When user types in an incorrect value, I show alert and then set the input value to the last correct input value.

Since I always want to display the value with one decimal number, I have bound the input to the function, which performs the formatting.

This is the part of the view:

<input data-bind="value: durationString" />

This is the part of the viewModel:

01.duration: 0;
02. 
03.//Converts string from the input to duration number and vice versa. Checks whether the string is a permitted number.
04.durationString: function (value) {
05.    if (value !== undefined) {
06.        var valueAsNumber = checkCorrectItemDuration(value);
07.        if (valueAsNumber !== -1) {
08.            this.set('duration', valueAsNumber); //new value is correct, set it
09.        }
10.        else {
11.            this.set('duration', this.get('duration')); //new value is incorrect, set the previous value
12.        }
13.    }
14.    else {
15.        return kendo.toString(this.get('duration'), 'n1');
16.    }
17.}

The problem is on the line 11. This line is executed when the value typed in the input is incorrect. I set the duration property to the same value as the value which it already has. I thought that this would force the input element to display the value from the duration property. But since the old and the new value of the duration property is the same, the input is not forced to refresh itself and so it still displays the incorrect value.

So, is it possible to force the input to display the correct value from the viewmodel?

Thanks for answers,

Boris

1 Answer, 1 is accepted

Sort by
0
Petyo
Telerik team
answered on 22 Jun 2016, 06:37 AM
Hello,

the logic you try to implement seems more suitable for another approach. Prevention of the input change event would do exactly what you want - revert the element value to its previous state.

Apart from that, "refresh" of the view is not supported. 

Regards,
Petyo
Telerik
 
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
 
Tags
MVVM
Asked by
Boris
Top achievements
Rank 1
Answers by
Petyo
Telerik team
Share this question
or