Prototype 1

[Pages:166]Prototype 1.5

The Complete API Reference

Sam Stephenson and the Prototype Team

Prototype 1.5: The Complete API Reference

Sam Stephenson and the Prototype Team

Published March 2007. 2nd edition. Copyright ? 2006-2007 Sam Stephenson. Some rights reserved.

Prototype1 is a JavaScript framework that aims to ease development of dynamic web applications. Prototype was created by Sam Stephenson who released the framework as an open-source project in February 2005. Other members of the core development team are: Thomas Fuchs, Justin Palmer, Andrew Dupont, Dan Webb, Scott Raymond, Seth Dillingham, Mislav Marohni#, Christophe Porteneuve and Tobie Langel. This PDF book version of the Prototype API reference was created by Josh Clark2, but all content comes from the Prototype site and is the intellectual property of Sam Stephenson and the Prototype core team. This PDF is distributed under the Creative Commons Attribution-ShareAlike 2.53. This means that you can copy, redistribute or create your own derivative works from this PDF, provided that you do so with the same Creative Commons license and include the foregoing notice. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.

1 2 3

Table of Contents

1. Utility Methods ....................................................................................................................1 $ ....................................................................................................................................1 $$ ..................................................................................................................................3 $A .................................................................................................................................4 $F ..................................................................................................................................5 $H .................................................................................................................................5 $R ..................................................................................................................................6 $w ..................................................................................................................................6 Try.these ........................................................................................................................7 document.getElementsByClassName .................................................................................8

2. Ajax ....................................................................................................................................9 Ajax Options ..................................................................................................................9 Ajax.PeriodicalUpdater ................................................................................................... 12 Ajax.Request ................................................................................................................. 15 Ajax.Responders ............................................................................................................ 19 Ajax.Updater ................................................................................................................. 20

3. Array ................................................................................................................................ 23 Why you should stop using for...in to iterate (or never take it up) .................................... 23 What is a developer to do? .............................................................................................. 24 clear ............................................................................................................................. 25 clone ............................................................................................................................ 25 compact ....................................................................................................................... 25 each ............................................................................................................................. 26 first .............................................................................................................................. 26 flatten ........................................................................................................................... 26 from ............................................................................................................................ 26 indexOf ........................................................................................................................ 27 inspect .......................................................................................................................... 27 last ............................................................................................................................... 28 reduce .......................................................................................................................... 28 reverse .......................................................................................................................... 28 size .............................................................................................................................. 29 toArray ......................................................................................................................... 29 uniq ............................................................................................................................. 29 without ......................................................................................................................... 30

4. Class ................................................................................................................................. 31 create ........................................................................................................................... 31

5. Element ............................................................................................................................ 33 addClassName .............................................................................................................. 34 addMethods .................................................................................................................. 34 ancestors ...................................................................................................................... 37

classNames ................................................................................................................... 38 cleanWhitespace ............................................................................................................ 38 descendantOf ................................................................................................................ 39 descendants .................................................................................................................. 40 down ............................................................................................................................ 40 empty ........................................................................................................................... 42 extend .......................................................................................................................... 42 getDimensions .............................................................................................................. 43 getElementsByClassName .............................................................................................. 43 getElementsBySelector ................................................................................................... 44 getHeight ...................................................................................................................... 45 getStyle ......................................................................................................................... 45 getWidth ...................................................................................................................... 46 hasClassName ............................................................................................................... 47 hide .............................................................................................................................. 47 immediateDescendants ................................................................................................... 48 inspect .......................................................................................................................... 49 makeClipping ................................................................................................................ 49 makePositioned ............................................................................................................. 50 match ........................................................................................................................... 51 next ............................................................................................................................. 51 nextSiblings .................................................................................................................. 53 observe ......................................................................................................................... 53 previous ....................................................................................................................... 54 previousSiblings ............................................................................................................ 55 readAttribute ................................................................................................................. 56 recursivelyCollect ........................................................................................................... 56 remove ......................................................................................................................... 57 removeClassName ......................................................................................................... 57 replace .......................................................................................................................... 58 scrollTo ........................................................................................................................ 59 setStyle ......................................................................................................................... 59 show ............................................................................................................................ 60 siblings ......................................................................................................................... 62 stopObserving ............................................................................................................... 62 toggle ........................................................................................................................... 63 toggleClassName ........................................................................................................... 64 undoClipping ................................................................................................................ 64 undoPositioned ............................................................................................................. 65 up ................................................................................................................................ 66 update .......................................................................................................................... 67 visible ........................................................................................................................... 69 6. Enumerable ....................................................................................................................... 71 Aliases: it's all about having it your way ............................................................................ 71 Using it efficiently .......................................................................................................... 71

vi | Prototype 1.5

collect, invoke, pluck and each: thinking about the use case ........................................ 72 reject and findAll vs. partition .............................................................................. 72 Mixing Enumerable in your own objects ......................................................................... 72 all ................................................................................................................................ 73 any ............................................................................................................................... 74 collect .......................................................................................................................... 75 detect ........................................................................................................................... 75 each ............................................................................................................................. 75 entries .......................................................................................................................... 77 find .............................................................................................................................. 77 findAll .......................................................................................................................... 78 grep ............................................................................................................................. 78 include ......................................................................................................................... 79 inject ............................................................................................................................ 79 invoke .......................................................................................................................... 80 map ............................................................................................................................. 81 max .............................................................................................................................. 81 member ........................................................................................................................ 82 min .............................................................................................................................. 82 partition ....................................................................................................................... 82 pluck ............................................................................................................................ 83 reject ............................................................................................................................ 83 select ............................................................................................................................ 84 size .............................................................................................................................. 84 sortBy .......................................................................................................................... 85 toArray ......................................................................................................................... 85 zip ............................................................................................................................... 86 7. Event ................................................................................................................................ 87 # What a wonderful mess (it would be) # ........................................................................ 87 Prototype to the rescue! .................................................................................................. 87 element ........................................................................................................................ 88 findElement .................................................................................................................. 88 isLeftClick .................................................................................................................... 89 observe ......................................................................................................................... 90 pointerX ....................................................................................................................... 92 pointerY ....................................................................................................................... 92 stop ............................................................................................................................. 93 stopObserving ............................................................................................................... 93 unloadCache ................................................................................................................. 95 8. Form ................................................................................................................................ 97 disable .......................................................................................................................... 97 enable .......................................................................................................................... 98 findFirstElement ........................................................................................................... 98 focusFirstElement ......................................................................................................... 98 getElements .................................................................................................................. 98

| vii

getInputs ...................................................................................................................... 99 reset ........................................................................................................................... 100 serialize ...................................................................................................................... 100 serializeElements ......................................................................................................... 101 9. Form.Element ................................................................................................................. 103 activate ....................................................................................................................... 103 clear ........................................................................................................................... 104 disable ........................................................................................................................ 104 enable ........................................................................................................................ 105 focus .......................................................................................................................... 105 getValue ..................................................................................................................... 105 present ....................................................................................................................... 105 select .......................................................................................................................... 106 serialize ...................................................................................................................... 106 10. Function ........................................................................................................................ 107 What is binding? .......................................................................................................... 107 Prototype to the rescue! ................................................................................................ 107 bind ........................................................................................................................... 107 bindAsEventListener ................................................................................................... 109 11. Hash ............................................................................................................................. 111 Creating a hash ............................................................................................................ 111 each ........................................................................................................................... 112 inspect ........................................................................................................................ 113 keys ........................................................................................................................... 113 merge ......................................................................................................................... 114 remove ....................................................................................................................... 114 toQueryString ............................................................................................................. 114 values ......................................................................................................................... 115 12. Insertion ........................................................................................................................ 117 After .......................................................................................................................... 117 Before ........................................................................................................................ 118 Bottom ....................................................................................................................... 118 Top ............................................................................................................................ 119 13. Number ........................................................................................................................ 121 What becomes possible ................................................................................................ 121 succ ........................................................................................................................... 122 times .......................................................................................................................... 122 toColorPart ................................................................................................................. 122 14. Object ........................................................................................................................... 123 clone .......................................................................................................................... 123 extend ........................................................................................................................ 124 inspect ........................................................................................................................ 124 keys ........................................................................................................................... 125 values ......................................................................................................................... 125 15. ObjectRange .................................................................................................................. 127

viii | Prototype 1.5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download