Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 108 lines (93 sloc) 3.708 kB
7f3d3d3 @shichuan added jquery plugin patterns
authored
1 <!doctype html>
2 <html lang="en">
3 <head>
4 <title>JavaScript Patterns</title>
5 <meta charset="utf-8">
6 </head>
7 <body>
f4561c1 @shichuan added better reference to jquery plugin patterns
authored
8 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
7f3d3d3 @shichuan added jquery plugin patterns
authored
9 <script>
10 /*!
11 * jQuery UI Widget-factory plugin boilerplate (for 1.8/9+)
12 * Author: @addyosmani
13 * Further changes: @peolanha
14 * Licensed under the MIT license
15 */
16
17 ;(function ( $, window, document, undefined ) {
18
19 // define your widget under a namespace of your choice
20 // with additional parameters e.g.
21 // $.widget( "namespace.widgetname", (optional) - an
22 // existing widget prototype to inherit from, an object
23 // literal to become the widget's prototype );
24
25 $.widget( "namespace.widgetname" , {
26
27 //Options to be used as defaults
28 options: {
29 someValue: null
30 },
31
32 //Setup widget (eg. element creation, apply theming
33 // , bind events etc.)
34 _create: function () {
35
36 // _create will automatically run the first time
37 // this widget is called. Put the initial widget
38 // setup code here, then you can access the element
39 // on which the widget was called via this.element.
40 // The options defined above can be accessed
41 // via this.options this.element.addStuff();
42 },
43
44 // Destroy an instantiated plugin and clean up
45 // modifications the widget has made to the DOM
46 destroy: function () {
47
48 // this.element.removeStuff();
49 // For UI 1.8, destroy must be invoked from the
50 // base widget
51 $.Widget.prototype.destroy.call(this);
52 // For UI 1.9, define _destroy instead and don't
53 // worry about
54 // calling the base widget
55 },
56
57 methodB: function ( event ) {
58 //_trigger dispatches callbacks the plugin user
59 // can subscribe to
60 // signature: _trigger( "callbackName" , [eventObject],
61 // [uiObject] )
62 // eg. this._trigger( "hover", e /*where e.type ==
63 // "mouseenter"*/, { hovered: $(e.target)});
64 this._trigger('methodA', event, {
65 key: value
66 });
67 },
68
69 methodA: function ( event ) {
70 this._trigger('dataChanged', event, {
71 key: value
72 });
73 },
74
75 // Respond to any changes the user makes to the
76 // option method
77 _setOption: function ( key, value ) {
78 switch (key) {
79 case "someValue":
80 //this.options.someValue = doSomethingWith( value );
81 break;
82 default:
83 //this.options[ key ] = value;
84 break;
85 }
86
87 // For UI 1.8, _setOption must be manually invoked
88 // from the base widget
89 $.Widget.prototype._setOption.apply( this, arguments );
90 // For UI 1.9 the _super method can be used instead
91 // this._super( "_setOption", key, value );
92 }
93 });
94
95 })( jQuery, window, document );
96
97
98 // References
f4561c1 @shichuan added better reference to jquery plugin patterns
authored
99 /*
100 The jQuery UI Widget Factory (by Adam J. Sontag) - http://goo.gl/DLKEt
101 Introduction to Stateful Plugins and the Widget Factory (by Doug Neiner) - http://goo.gl/eTGhP
102 Widget Factory (by Scott González) - http://goo.gl/5qFQG
103 Understanding jQuery UI widgets: A tutorial (by Hacking at 0300) - http://goo.gl/rBhaF
104 Essential jQuery Plugin Patterns (by Addy Osmani) - http://goo.gl/oE0ge
105 */
7f3d3d3 @shichuan added jquery plugin patterns
authored
106 </script>
107 </body>
108 </html>
Something went wrong with that request. Please try again.