tailieunhanh - Practical prototype and scipt.aculo.us part 21

Practical prototype and part 21: The information in this book is distributed on an "as is" basis, without warranty Although every pre-caution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. | CHAPTER 5 EVENTS 109 custom events from anywhere in your code you can also listen for custom events with the same API that you d use to listen for native browser events. The First Custom Event Prototype itself fires a custom event called dom loaded. It fires at a specific time in the page s life cycle after the page s DOM tree is fully accessible to scripts but before the window s load event which doesn t fire until all external assets . images have been fully downloaded. Use dom loaded when you want to work with the DOM in that narrow window of time before the page appears on the screen fully rendered. In nearly all cases it s better to assign to dom loaded than load unless your handler depends upon having everything downloaded and rendered. This is also a good time to talk about the naming scheme for custom events. You ve probably noticed that dom loaded unlike native events contains a colon. This is by design all custom events must contain a colon in their names. Since custom events are handled differently under the hood Prototype needs a way to distinguish them from native browser events which number in the hundreds if all major browsers are considered . Embrace the convention. Broadcasting Scores The data stream we built in Chapter 4 will power a large portion of our fantasy football site. It would be wasteful and silly for each JavaScript component to make its own Ajax requests so let s write some general code with the specific task of asking for scores from the server and then announcing these scores through some sort of public address system. Create a new file called and place this code inside var ScoreBroadcaster setup function new PeriodicalExecuter this 30 update function new onSuccess this 110 CHAPTER 5 EVENTS success function request score updated dom loaded function .

TỪ KHÓA LIÊN QUAN