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

toggle() and set() don't fire MVVM change event

5 Answers 534 Views
Switch
This is a migrated thread and some comments may be shown as answers.
Jay
Top achievements
Rank 2
Iron
Iron
Veteran
Jay asked on 11 Mar 2020, 07:48 PM

The switch's toggle() method doesn't fire the MVVM change event, nor does it update the viewModel property.

Setting the viewModel property obviously updates the viewModel property, but it too doesn't fire the change event.

Only clicking on the switch seems to fire it.

Both of the methods do change the visual state appropriately, and also end up changing the aria-checked value. Here is a dojo.

Is there a way to programmatically change the switch that also fires the change event?

5 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Danchev
Telerik team
answered on 13 Mar 2020, 02:14 PM

Hello Jay,

Invoking the toggle method does not trigger the change event. This is something fairly common for the widgets API methods in general, for example programmatically selecting a value in the dropdown widgets also does not trigger their change event.

To make the event fire trigger it after calling the respective API method. In this case trigger the change event after invoking toggle():

switchInstance.trigger("change", { checked: switchInstance.check()} )

Modified dojo: https://dojo.telerik.com/aLIVinur

Regards,
Ivan Danchev
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Jay
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 13 Mar 2020, 04:34 PM
Hi Ivan. Thanks, that will work. But speaking of commonality across widgets, I've noticed the the switch's change event is fired before it updates the viewModel, while the numerictextbox's change event is fired after it updates the viewModel (I haven't checked any others). Is there a general consistency across the widgets as to the order of updating the viewModel and firing the change event and it just so happens that one of these is atypical? Or is it just up to the individual widget and one just needs to be aware that it may be one way or the other?
0
Accepted
Ivan Danchev
Telerik team
answered on 17 Mar 2020, 04:07 PM

Hi Jay,

Although such consistency across the widgets is not documented, the Switch is an exception to the rule, because for the other widgets of the Editors group (DropDowns, Pickers, etc.) the widget's change event fires after the viewModel's change event.

Regards,
Ivan Danchev
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Joshua
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 27 Nov 2020, 05:59 PM

With it the way it is it defeats the purpose of the view model. I have lots of nested widgets. At my higher level widgets, they don't know which widgets are below them, so they don't have access to update them directly. They have access to the view model. This means that when I want to control state, I update my view model and those changes should propagate to widgets lower in the tree.

 

Is there anyway to override this functionality or work around it? Changing a value in the view model should cause the change event to fire for the control, as I would expect?

0
Ivan Danchev
Telerik team
answered on 01 Dec 2020, 04:20 PM

Joshua,

We are not aware of a viable workaround to trigger the change event, other that calling the widget's trigger method, as previously suggested. Customizing the widgets to fire "change" automatically on view model value changes, would require source code modifications.

Regards,
Ivan Danchev
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Switch
Asked by
Jay
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Ivan Danchev
Telerik team
Jay
Top achievements
Rank 2
Iron
Iron
Veteran
Joshua
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or