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
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
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
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?
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/.