【例子介绍】Extending.jQuery.Aug.2013.pdf.part1
【相关图片】
【源码结构】
ontents
foreword xiv
preface xvi
acknowledgments xviii
about this book xix
about the cover illustration xxiii
P ART 1 S IMPLE EXTENSIONS . ....................................... 1
1
jQuery extensions 3
1.1 jQuery background 4
Origins 4
■
Growth 5
■
Today 7
1.2 Extending jQuery 8
What can you extend? 8
1.3 Extension examples 11
jQuery UI 11
■
Validation 12
■
Graphical slider 13
Google Maps integration 14
■
Cookies 14
■
Color animation 15
1.4 Summary 15
Download from Wow! eBook <www.wowebook.com>
CONTENTS viii
2
A first plugin 17
2.1 jQuery architecture 18
jQuery extension points 19
■
Selectors 20
■
Collection
plugins 21
■
Utility functions 21
■
jQuery UI widgets 21
jQuery UI effects 22
■
Animating properties 22
■
Ajax
processing 22
■
Events handling 23
■
Validation rules 24
2.2 A simple plugin 24
Placeholder text 24
■
Watermark plugin code 25
■
Clearing
the watermarks 26
■
Using the Watermark plugin 27
2.3 Summary 29
3
Selectors and filters 30
3.1 What are selectors and filters? 31
Why add new selectors? 31
■
Basic selectors 32
Pseudo-class selectors 33
3.2 Adding a pseudo-class selector 37
The structure of a pseudo-class selector 37
■
Adding an exact
content selector 39
■
Adding a pattern matching content
selector 41
■
Adding element type selectors 43
■
Adding a
foreign language selector 43
■
Selectors from the Validation
plugin 45
3.3 Adding a set filter 45
The structure of a set selector 46
■
Adding a middle elements set
selector 47
■
Enhancing the equals selector 48
3.4 Summary 50
P ART 2 P LUGINS AND FUNCTIONS .............................. 51
4
Plugin principles 53
4.1 Plugin design 54
Plugin benefits 54
■
Planning the design 54
Modularize the plugin 56
4.2 Guiding principles 56
Provide progressive enhancements 56
■
Only claim a single
name and use that for everything 57
■
Place everything under
the jQuery object 57
■
Don’t rely on $ being the same as
jQuery 58
■
Hide the implementation details by using
scope 58
■
Invoke methods for additional functionality 59
Return the jQuery object for chaining whenever possible 60
Download from Wow! eBook <www.wowebook.com>
CONTENTS ix
Use the data function to store instance details 60
Anticipate customizations 61
■
Use sensible defaults 62
Allow for localisation/localization 63
■
Style your plugin with
CSS 64
■
Test on the major browsers 66
■
Create a
repeatable test case suite 66
■
Provide demonstrations and
documentation 67
4.3 Summary 69
5
Collection plugins 70
5.1 What are collection plugins? 71
5.2 A plugin framework 71
The MaxLength plugin 71
■
MaxLength plugin operation 72
5.3 Defining your plugin 74
Claiming a namespace 74
■
Encapsulation 74
■
Using a
singleton 75
5.4 Attaching to an element 76
Basic attachment 77
■
Plugin initialization 78
■
Invoking
methods 79
■
Getter methods 80
5.5 Setting options 82
Plugin defaults 82
■
Localisations/localizations 83
Reacting to option changes 85
■
Implementing MaxLength
options 86
■
Enabling and disabling the widget 88
5.6 Adding event handlers 89
Registering an event handler 89
■
Triggering an event
handler 90
5.7 Adding methods 90
Getting the current length 90
5.8 Removing the plugin 91
The destroy method 91
5.9 Finishing touches 92
The plugin body 92
■
Styling the plugin 94
5.10 The complete plugin 95
5.11 Summary 96
6
Function plugins 97
6.1 Defining your plugin 98
Localization plugin 98
■
Framework code 99
■
Loading
localizations 100
Download from Wow! eBook <www.wowebook.com>
CONTENTS x
6.2 jQuery Cookie plugin 103
Cookie interactions 103
■
Reading and writing cookies 104
6.3 Summary 106
7
Test, package, and document your plugin 107
7.1 Testing your plugin 108
What to test? 108
■
Using QUnit 109
■
Testing the
MaxLength plugin 111
■
Testing option setting and
retrieval 113
■
Simulating user actions 114
■
Testing
event callbacks 116
7.2 Packaging your plugin 117
Collating all the files 117
■
Minimizing your plugin 118
Providing a basic example 121
7.3 Documenting your plugin 123
Documenting options 123
■
Documenting methods and utility
functions 124
■
Demonstrating your plugin’s abilities 125
7.4 Summary 127
P ART 3 E XTENDING J Q UERY UI ............................... 129
8
jQuery UI widgets 131
8.1 The widget framework 132
jQuery UI modules 132
■
The Widget module 134
The MaxLength plugin 135
■
MaxLength plugin
operation 136
8.2 Defining your widget 137
Claiming a name 137
■
Encapsulating the plugin 137
Declaring the widget 138
8.3 Attaching the plugin to an element 139
Basic attachment and initialization 139
8.4 Handling plugin options 141
Widget defaults 141
■
Reacting to option changes 142
Implementing MaxLength options 144
■
Enabling and
disabling the widget 147
8.5 Adding event handlers 147
Registering an event handler 148
■
Triggering an event
handler 148
Download from Wow! eBook <www.wowebook.com>
CONTENTS xi
8.6 Adding methods 150
Getting the current length 150
8.7 Removing the widget 151
The _destroy method 151
8.8 Finishing touches 152
The widget body 152
■
Styling the widget 154
8.9 The complete plugin 155
8.10 Summary 158
9
jQuery UI mouse interactions 159
9.1 The jQuery UI Mouse module 160
Mouse-drag actions 160
■
Mouse options 161
9.2 Defining your widget 161
Signature functionality 161
■
Signature plugin
operation 163
■
Declaring the widget 164
9.3 Attaching the plugin to an element 165
Framework initialization 165
■
Custom initialization 166
9.4 Handling plugin options 167
Widget defaults 167
■
Setting options 169
■
Implementing
Signature options 170
■
Enabling and disabling the
widget 171
9.5 Adding event handlers 171
Registering an event handler 171
■
Triggering an event
handler 172
9.6 Interacting with the mouse 173
Can a drag start? 173
■
Starting a drag 174
■
Tracking a
drag 174
■
Ending a drag 175
9.7 Adding methods 176
Clearing the signature 176
■
Converting to JSON 177
Redrawing the signature 178
■
Checking signature
presence 179
9.8 Removing the widget 179
The _destroy method 180
9.9 The complete plugin 180
9.10 Summary 181
Download from Wow! eBook <www.wowebook.com>
CONTENTS xii
10
jQuery UI effects 182
10.1 The jQuery UI effects framework 183
The Effects Core module 183
■
Common effects functions 184
Existing effects 186
10.2 Adding a new effect 188
Imploding an element 188
■
Initializing the effect 189
Implementing the effect 191
■
Implementing an effect prior to
jQuery UI 1.9 192
■
The complete effect 193
10.3 Animation easings 194
What’s an easing? 194
■
Existing easings 195
Adding a new easing 197
10.4 Summary 199
P ART 4 O THER EXTENSIONS .................................... 201
11
Animating properties 203
11.1 The animation framework 204
Animation capabilities 204
■
Stepping an animation 206
11.2 Adding a custom property animation 208
Animating background-position 209
■
Declaring and
retrieving the property value 210
■
Updating the property
value 212
■
Animating background-position in
jQuery 1.7 213
■
The complete plugin 215
11.3 Summary 215
12
Extending Ajax 216
12.1 The Ajax framework 217
Prefilters 218
■
Transports 218
■
Converters 219
12.2 Adding an Ajax prefilter 220
Changing the data type 220
■
Disabling Ajax processing 220
12.3 Adding an Ajax transport 221
Loading image data 221
■
Simulating HTML data for
testing 223
12.4 Adding an Ajax converter 226
Comma-separated values format 227
■
Converting text to
CSV 227
■
Converting CSV to a table 230
Download from Wow! eBook <www.wowebook.com>
CONTENTS xiii
12.5 Ajax plugins 231
12.6 Summary 232
13
Extending events 233
13.1 The special event framework 234
Binding event handlers 234
■
Triggering events 235
13.2 Adding a special event 236
Adding a right-click event 236
■
Disabling right-click
events 238
■
Multiple right-click events 239
Collection functions for events 242
13.3 Enhancing an existing event 243
Adding right-click handling to the click event 244
13.4 Other event functionality 245
Default actions for events 245
■
Pre- and post-dispatch
callbacks 246
■
Prevent event bubbling 247
Automatic binding and delegation 247
13.5 Summary 249
14
Creating validation rules 250
14.1 The Validation plugin 251
Assigning validation rules 252
14.2 Adding a validation rule 253
Adding a pattern-matching rule 254
■
Generating pattern-
matching rules 256
14.3 Adding a multiple-field validation rule 258
Grouping validations 258
■
Defining a multiple-field
rule 259
14.4 Summary 261
appendix Regular expressions 263
glossary 273
index 277
Download from Wow! eBook <www.wowebook.com>
xiv
foreword
评论