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

4 posts, 2 answers
  1. Jay
    Jay avatar
    84 posts
    Member since:
    Dec 2011

    Posted 11 Mar Link to this post

    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?

  2. Answer
    Ivan Danchev
    Admin
    Ivan Danchev avatar
    2089 posts

    Posted 13 Mar Link to this post

    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.
  3. Jay
    Jay avatar
    84 posts
    Member since:
    Dec 2011

    Posted 13 Mar in reply to Ivan Danchev Link to this post

    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?
  4. Answer
    Ivan Danchev
    Admin
    Ivan Danchev avatar
    2089 posts

    Posted 17 Mar Link to this post

    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.
Back to Top