4.3. Handling Control EventsAtlas provides its client controls with an event handling mechanism. The mechanism works a bit differently than you might expect, but it's still intuitive. The first and most important step is to call the initialize() method of the element whose events you want to handle. This enables all the mechanisms that are internally used to capture events. Then setting up events is a two-step process.
4.3.1. Events for ButtonsRemember the example with the three modal pop-up windows from the beginning of this chapter? There, the JavaScript code to display the windows was added declaratively in the HTML button. This can also be done using the Atlas library, but in that case, you do not gain much from using Atlas in comparison to the "pure" JavaScript way, except for the certainty that the Atlas library is fully loaded before attaching any JavaScript code to an element. However, the whole idea of the Atlas framework is to bring server-side and client-side development closer to each other and to bring new OOP capabilities and browser independence to the client. Therefore, using Atlas for tasks that you can do as easily in JavaScript still has benefits. Example 4-9 revisits the "three windows" example from Example 4-1, using Atlas event handling. The HTML buttons are referenced using the Sys.UI.Button class, and the associated event is (obviously) click. Example 4-9. Using Atlas Button control events
4.3.2. Events for ListsAn event that is implemented for many Atlas client controls, one that does not exist in this form in JavaScript, is propertyChanged. It is used generically for all controls to indicate that something has changed: a key was pressed, a list item was selected, and so on. It is also possible to work with individual change events for each form element so that you know exactly what has changed. For instance, when the selected element in a selection list changes, it raises the selectionChanged event (in JavaScript, the event is called change). Illustrating this event is once again an opportunity to rewrite one of the previous examples (see Example 4-7). This time, we do not have to periodically check the selection list for changes; instead, we capture the associated event. Remember to call initialize(); otherwise, the event cannot be captured. Example 4-10 shows code that handles a Select control's selectionChanged event. Example 4-10. Using Atlas selection list events
The performance of this code is much better than in the previous version of this example, since the application reacts immediately when the selection in the list is changed and not just at the end of each 1,000-millisecond interval. |