If you have a custom form widget, and want to prevent changes being lost when a user clicks a tab, what is the best way to do this? I tried to use the “beforeunload” event but it doesn’t seem to work when navigating using links , subtabs or top tabs.
Although there is no “official” way to hook a Code Studio widget into Create’s logic for unsaved changes this (but you should raise an Idea for that, as it would be useful), I think you could get it to work as follows:
- Make sure the “Abandon prompt” setting is enabled on your Widget Group, this activates Create’s own unsaved changes warning logic.
- Within your widget code, when form changes are made which should trigger the warning, you need to add the “has_changes“ class to the widget group container which contains your widget. Assuming you have only one widget group on the page this could be:
$('form.page_widget_group.abandon_prompt').addClass('has_changes');- This should then trigger Create’s unsaved changes warning message when you try to navigate away.
Note that because this is not an official feature, there is always the risk that the way this works in Create may change in a future version and your code would stop working at that point.