From 455fbe008773c56263cebb8c17d73cb1537bfb5a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=CC=88rn=20Zaefferer?= jQuery UI widgets are created using If all you want is one-time initialization and calling methods, your object passed to All you need here is a function that acts as the constructor, accepting two arguments: You can then hook this object up as a jQuery plugin using the bridge and use it on any jQuery object: To use all the features of the bridge, your object also needs to have an There is one optional property the bridge will use, if present: If your object's prototype has a jQuery.widget.bridge() method. This acts as the middleman between the object created by $.widget() and the jQuery API.$.widget.bridge() does a few things:
+
+ $.data cache.$.widget( "foo.bar", {} ); syntax to define an object from which instances will be created. Given a DOM structure with five .foo's, $( ".foo" ).bar(); will create five instances of your "bar" object. $.widget.bridge() works inside the factory by taking your base "bar" object and giving it a public API. Therefore, you can create instances by writing $( ".foo" ).bar(), and call methods by writing $( ".foo" ).bar( "baz" ).jQuery.widget.bridge() can be very minimal:
+
+ var Highlighter = function( options, element ) {
+ this.options = options;
+ this.element = $( element );
+ this._set( 800 );
+ };
+ Highlighter.prototype = {
+ toggle: function() {
+ this._set( this.element.css( "font-weight") === 400 ? 800 : 400 );
+ },
+ _set: function(value) {
+ this.element.css( "font-weight", value );
+ }
+ };
+
+
+ options: an object of configuration optionselement: the DOM element this instance was created on
+
+ // Hook up the plugin
+ $.widget.bridge( "colorToggle", Highlighter );
+
+ // Initialize it on divs
+ $( "div" ).colorToggle().click(function() {
+ // Call the public method on click
+ $( this ).colorToggle( "toggle" );
+ });
+ _init() method on the prototype. This will get called whenever the plugin is invoked while an instance already exists. In that case you also need to have an option() method. This will be invoked with the options as the first argument. If there were none, the argument will be an empty object. For a proper implementation of the option method, check out the implementation of $.Widget.widgetFullName property, this will be used as the key for storing and retrieving the instance. Otherwise, the name argument will be used.