1+ <!doctype html>
2+ < html lang ="en ">
3+ < head >
4+ < title > JavaScript Patterns</ title >
5+ < meta charset ="utf-8 ">
6+ </ head >
7+ < body >
8+ < script >
9+ /*!
10+ * jQuery UI Widget-factory plugin boilerplate (for 1.8/9+)
11+ * Author: @addyosmani
12+ * Further changes: @peolanha
13+ * Licensed under the MIT license
14+ */
15+
16+ ; ( function ( $ , window , document , undefined ) {
17+
18+ // define your widget under a namespace of your choice
19+ // with additional parameters e.g.
20+ // $.widget( "namespace.widgetname", (optional) - an
21+ // existing widget prototype to inherit from, an object
22+ // literal to become the widget's prototype );
23+
24+ $ . widget ( "namespace.widgetname" , {
25+
26+ //Options to be used as defaults
27+ options : {
28+ someValue : null
29+ } ,
30+
31+ //Setup widget (eg. element creation, apply theming
32+ // , bind events etc.)
33+ _create : function ( ) {
34+
35+ // _create will automatically run the first time
36+ // this widget is called. Put the initial widget
37+ // setup code here, then you can access the element
38+ // on which the widget was called via this.element.
39+ // The options defined above can be accessed
40+ // via this.options this.element.addStuff();
41+ } ,
42+
43+ // Destroy an instantiated plugin and clean up
44+ // modifications the widget has made to the DOM
45+ destroy : function ( ) {
46+
47+ // this.element.removeStuff();
48+ // For UI 1.8, destroy must be invoked from the
49+ // base widget
50+ $ . Widget . prototype . destroy . call ( this ) ;
51+ // For UI 1.9, define _destroy instead and don't
52+ // worry about
53+ // calling the base widget
54+ } ,
55+
56+ methodB : function ( event ) {
57+ //_trigger dispatches callbacks the plugin user
58+ // can subscribe to
59+ // signature: _trigger( "callbackName" , [eventObject],
60+ // [uiObject] )
61+ // eg. this._trigger( "hover", e /*where e.type ==
62+ // "mouseenter"*/, { hovered: $(e.target)});
63+ this . _trigger ( 'methodA' , event , {
64+ key : value
65+ } ) ;
66+ } ,
67+
68+ methodA : function ( event ) {
69+ this . _trigger ( 'dataChanged' , event , {
70+ key : value
71+ } ) ;
72+ } ,
73+
74+ // Respond to any changes the user makes to the
75+ // option method
76+ _setOption : function ( key , value ) {
77+ switch ( key ) {
78+ case "someValue" :
79+ //this.options.someValue = doSomethingWith( value );
80+ break ;
81+ default :
82+ //this.options[ key ] = value;
83+ break ;
84+ }
85+
86+ // For UI 1.8, _setOption must be manually invoked
87+ // from the base widget
88+ $ . Widget . prototype . _setOption . apply ( this , arguments ) ;
89+ // For UI 1.9 the _super method can be used instead
90+ // this._super( "_setOption", key, value );
91+ }
92+ } ) ;
93+
94+ } ) ( jQuery , window , document ) ;
95+
96+
97+ // References
98+ // http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/
99+ </ script >
100+ </ body >
101+ </ html >
0 commit comments