jcompagner wrote:why don't you use a service for this?
That is the easiest and very clean
do try to avoid "window" (use $window) so for document use $window.document
I first thought of a service. However there are 2 problems with that.
1. There isn't any way that I know of in the Servoy API to somehow allow the user to assign the Servoy method to the handler. So if I used a service, I would have to hard code the Servoy method name it runs, which I prefer to avoid. So using the component, it can be placed on a form, and then the user can assign their Servoy method to the handler.
2. I could have made an API call in the service, like setHandler that the user could call, and set what Servoy method they wanted. Then I would call it. However, that wouldn't be reliable I think. When would the user trigger that to run? Maybe onSolutionOpen, but would that fire before the onReady in the service? Seems to capable of reliability issues since the handler would be set at runtime instead of design time, and maybe not set soon enough in all situations.
2 problems with that.
1. That only works if the solution doesn't require login. The onOpen event will run on the Login Solution on first launch. You could make a special solution, not requiring login, but I think that would still be a bit of a security hole. I need to trigger an event on the link but in a login solution.
2. That doesn't handle the user clicking refresh on their browser. Servoy rewrites the URL after that loads, so a browser refresh wouldn't trigger the method to run again.
You could make an argument that the onShow function should really work the way I'm describing onReady. For example
- User opens a session, form shows. onShow runs.
- User closes the tab in their browser.
- User opens another tab, loads the solution again. Their session state remains, and the form they left off on is shown to them. However the onShow doesn't run! Its always been that way, dating back to the first version of the WebClient.
So, I didn't want to request a feature that would break existing functionality, in case some users rely on the onShow not running. However, if you think of normal web development, everyone uses the onReady style, so if the user has multiple windows, close and reopens, or does browser refreshes, your event will always run.
In simpler terms, think of it like this. There is no way in Servoy to always have a method run every time some form is shown to the user.
- onShow doens't run if the user clicks refresh, or closes and reopens tabs in browser
- onSolutionOpen only runs first time opening
- Using method args in the URL only run if they click that url, not if the click refresh, or closes and reopens tabs in browser.
For developers coming from other standards based Web Development backgrounds, that concept is pretty confusing. We should really have some event that more closely matches the jQuery onReady style event. Especially since the ngClient is Web only! I could understand not doing it in the Web/Smart client since apps were built for both desktop and browser. However the ngClient is Web Only, so we should really conform to standards that web developers are used to.