Save changes popup when Switching tabs in VLF One

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
VLNinja70

Save changes popup when Switching tabs in VLF One

Post by VLNinja70 »

Hello Forum!
I'm running into an issue with the web framework where I'm trying to stop the user from going to another tab in the command handler without saving their changes.
I've looked at using a method routine thats part of the command handler called uQueryCanClose but I can't get it to do exactly what I want.
http://docs.lansa.com/14/en/lansa048/Co ... 8_4830.htm

It doesn't seem to trigger on tab switches, just if you close the command handler window itself.
I saw in the doco that you can setup logic based on the reason code returned from the method, but it looks like I cant do that in the web :(

I turned on tracing to see what else is going on when I switch to a different tab and I notice that uTerminate runs.
So, i put in some catching logic when the user switches tabs, if something has changes go to uQueryCanClose through uTerminate.
But now I get a pop up from the browser that says the framework tried to destroy is still active. I don't want my users seeing this.

Also, a side effect i noticed when using uQueryCanClose is if the command handler detects it I can no longer close the command handler no matter what I do. Could be just my coding, not really sure at this point :D

Anyway, here is all the important bits from my command handler that listens for the changes.

Code: Select all

Mthroutine Name(uTerminate) Options(*REDEFINE)

* <your termination logic goes here>
* <your termination logic goes here>
* <your termination logic goes here>
If Cond(#Button_All.Enabled *EQ TRUE)
#SYS_WEB.Alert Caption("Called through uTerminate")
Invoke Method(#COM_OWNER.uQueryCanClose) Canbecancelled(False) Timeoutinprogress(False)
Endif

#PanelFields.RemoveAll
#KeyFields.RemoveAll

* Do any termination defined in the ancestor
#Com_Ancestor.uTerminate

Endroutine

Mthroutine Name(uQueryCanClose) Options(*REDEFINE)
If Cond(#Button_All.Enabled *EQ True)
#SYS_WEB.Alert Caption("Called through uQueryCanClose")
#SAVEMSGBOX.Show
Else
#COM_OWNER.uTerminate
Endif
Endroutine

Evtroutine Handling(#SAVEMSGBOX.Closed)
If Cond(#SAVEMSGBOX.Result *EQ YES)
* RUN THE SAVE LOGIC
#COM_OWNER.uSave
#SYS_WEB.Alert Caption("Logic Saved!")
* Otherwise, skip the save and load the next screen
Else
#Button_All.Enabled := False
#COM_OWNER.uTerminate
Endif
Endroutine
Its all based on a generic CRUD handler tapping off of PSLMST (demo employee file).

Thanks forum! :P
csturtev
Posts: 12
Joined: Thu Feb 02, 2017 6:54 am

Re: Save changes popup when Switching tabs in VLF One

Post by csturtev »

I have encounter the same issue almost word for word. I also have a call open with support on this, and they have not provided a way to accomplish this yet. Very interested in knowing if anyone has a way to accomplish this.
MarkD
Posts: 692
Joined: Wed Dec 02, 2015 9:56 am

Re: Save changes popup when Switching tabs in VLF One

Post by MarkD »

There's a number of considerations here.

You really should not be invoking method uTerminate from your own code.
The VLF engine will invoke uTerminate at the correct time - if you do it yourself then the shutdown sequence is likely to get messed up.

That might be why you are seeing the leakage warning. Your end users would not see that message. It only appears if you have Develoepr=Y on the URL, but it's definitely not something to ignore.

I would not call uQueryCanClose from your own code either for the same reason.

Maybe have a look at how the Stay Active option works?
This allows your command handler to stay active when another command handler on another tab fires up.
That has the potential to change how and when you need to save unsaved changes.
MarkD
Posts: 692
Joined: Wed Dec 02, 2015 9:56 am

Re: Save changes popup when Switching tabs in VLF One

Post by MarkD »

Where changes cannot be saved because they contain validation errors, handling this can get a little complex.

Sometimes you can only abandon unsaved changes, and sometimes you cannot reliably save changes because the whole web browser window is being closed and it won’t allow any more HTTP/S requests.

Maybe start out to experiment with what fits your needs with a little method like this:

Code: Select all

Mthroutine Name(SaveUnsavedChanges)
Define_Map For(*INPUT) Class(#prim_boln) Name(#AskUserFirst)
Define_Map For(*INPUT) Class(#prim_boln) Name(#AbandonChangesonFailure)
Define_Com Class(#Prim_Boln) Name(#AttemptDataSave) Value(True)
If (#Button_Save.Enabled) /* Data is ‘dirty’ and needs to be saved */
If (#AskUserFirst)
#AttemptDataSave := (#SYS_WEB.Confirm( ('Changed details of ' + #XXXXXXXXXX + ' have not been saved. Do you want to save them now?') ) = OK)
Endif
If (#AttemptDataSave)
* << Attempt to save >>
Endif
Endif
#Button_Save.Enabled := False
Endroutine
then look at the points where you might need to invoke it ……….

When the Save button is clicked. Called with AskUserFirst(False) abandonChangesonFailure(false)

When uExecute is executed again when the user clicks on a different item in the instance list or on the command handlers tab again. Probably called with AskUserFirst(True) abandonChangesonFailure(True).

When uQueryCanClose is executed, probably with askuserFirst passed to match the #CanBeCancelled parameter, and abandonChangesonFailure(True). Note that if the user closes the whole browser with the top right red X you may not always be allowed to attempt a save anyway. That's why having a logoff or Exit button on your toolbar is important because it allows for a more structured and controllable shutdown.

You can’t currently stop a user going to another command handler tab - so how you react to unsaved data in that situation depends on whether your command handler stays active or not.

If it stays active you can just defer the required save until the command handler is later asked to close, or to display a different data item via another uExecute execution. I think the shipped employee demo works that way.

If your command handler does not stay active, you may be able to set up your own redefined uDeActivate method and invoke your version of SaveUnsavedchanges from there. You can attempt ask the user I think, but you will have to abandon invalid data in this context.

The key thing is that there are situations where you can’t stop what is going to happen.
Sometimes you can only attempt a save and need to be prepared to abandon or lose them.

I think we could simplify this a bit in a future version – but it’s not clear what would be the best way to do that, so any suggestions would be great.
jyoung
Posts: 694
Joined: Thu Jan 21, 2016 6:43 am
Location: Oklahoma City, OK USA

Re: Save changes popup when Switching tabs in VLF One

Post by jyoung »

I am running into the same issue.

The way I am understanding it is that the uQueryCanClose only gets invoked when the command window is closed (via the "X" on the window or "exiting" via the Tool Bar).
It DOES NOT get invoked when a user clicks on another tab.

Is that correct?

I've got a simple check in the uQueryCanClose

Code: Select all

mthroutine name(uQueryCanClose) options(*REDEFINE)

* the save button is enabled when there are changes on the form
if ((#avListManager.CurrentInstance *IsNot *NULL) *And (#SaveButton.Enabled))
* we have changes, does the user want to abandon them
if (#SYS_WEB.Confirm( "You have unsaved changes! Click 'Cancel' to save them or 'Ok' to abandon them." ) = Cancel)
#Allow := False
else
#Allow := True
endif
else
* no changes detected, allow the command to be closed
#Allow := True
endif
endroutine
As VLNinja and csturtev mentioned, it does not seem to be invoked when the tab changes.
I am fiddling around with uDeActivate, but not having much luck at it either.

IMHO, I think that uQueryCanClose or a cancelable "uTabActivated/DeActivated" method be invoked on tab changes.
MarkD
Posts: 692
Joined: Wed Dec 02, 2015 9:56 am

Re: Save changes popup when Switching tabs in VLF One

Post by MarkD »

uQueryCanClose gets activated when closing the browser, when closing/exiting the framework, or when closing the pane that contains the command handler's tab. I think if you set your command handler to Stay Active then uQueryCanClose will handle all the save situations and you won't lose data moving to other tabs because the command handler stays alive until it gets closed.
MarkD
Posts: 692
Joined: Wed Dec 02, 2015 9:56 am

Re: Save changes popup when Switching tabs in VLF One

Post by MarkD »

Sorry, I have misled you about uDeactivat. It's no help here.
It only happens when a command handler (or filter) gets completely removed from view.
That's only when you switch to another application's work sheet, or minimize the pane containing the command handler.
VLNinja70

Re: Save changes popup when Switching tabs in VLF One

Post by VLNinja70 »

Thanks guys!
Mark, I will give the stay active option a go.
The other thing I may try is in my uTermination logic, I can save off my entries if they were not saved and just bring them back up when the user goes to that tab again.
Kind of like when word or chrome crashes, when you open it back up its asks "Hey, do you want to recover the last doc?" or recover the closed tabs?
Maybe something like that? Since I can't stop the user from leaving or closing up the browser.

I'll post the code if I get it working :D
Post Reply