Apparently, there is a part2 in my struggle with this ‘current user hyperlink’ that I inserted in my LightSwitch HTML client as a custom ‘control’…
At first sight, the link works perfectly: when you click it, the app shows a popup where you can edit your profile. Because of the databinding, the hyperlink will automatically update its text when the user edits his/her name in that dialog.
At first sight…
Once I added multiple screens to my application, I noticed that the hyperlink databinding behaves correctly when I first open the screen, but after navigating to another screen and then back again, my custom control stopped working. In fact, after navigating to another screen and then back again, half of my custom controls were behaving funny, the other half was just fine. And by the time I noticed this subtlety, I had way too much custom controls on my screen to log it as a ‘known issue’ ;-)
I don’t remember ever running into problems with this before, but apparently there is a subtle difference in behavior between two different ways I interact with my custom ‘controls’. Zooming in on the ‘offending’ code reveals the problem (when you know where to look, bugs are always easier to find):
I create the usercontrol via a JQuery selector, then save a ‘reference to the usercontrol’ in a variable. In my first interaction, I use this variable directly. For some reason, in my second interaction, I use a new JQuery selector to get a new ‘reference to the same usercontrol’.
This seems to work fine for the very first time, but breaks silently on subsequent rendering.
The first fix seems to be the obvious: use the variable instead of using a new JQuery selector.
A second fix that seems to work, is to start the JQuery selector using the parent element (passed as an argument to your _render or _postRender functions) as the context:
If anyone can enlighten me as the technical reasoning behind this issue, I’d love to hear it :-) Until I understand, I think I’ll stick with this last approach because it seems to be the most stable one…
Keep rocking LS!