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

Change events for related fields

1 Answer 68 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
jmillar
Top achievements
Rank 1
jmillar asked on 17 Nov 2014, 06:19 PM
Hi all;

I'm working on a project that has a model with dependent fields.  As an example, consider a model with fields for both metric and imperial length (for my purpose, they both need to be writable fields).  So when one value changes, I need to change the other as well -- I'm using MVVM to bind both of these to a Kendo view.  My first attempt was to bind a function to the set event of each in the Kendo observable object, but I have a hunch that's going to lead to an infinite loop (changing metric, leading to changing imperial, leading to changing metric, etc.)  Am I going about this the wrong way?

Thanks,
Jason

    var oc = kendo.observable({
        MetricLength: 1,
        ImpLength: 3.28
    });

    oc.bind("set", function(e) {
        if (e.field == "MetricLength") {
            oc.set("ImpLength",e.value * 3.28);
        }
        if (e.field == "ImpLength") {
            oc.set("MetricLength",e.value*3.28);
        }
    })

1 Answer, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 19 Nov 2014, 11:33 AM
Hi Jason,

In the current scenario I would suggest using flags to avoid entering an infinite loop. Here is a small example how that could be done.

Regards,
Alexander Popov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
MVVM
Asked by
jmillar
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Share this question
or