Bridget makes jQuery plugins
Bridget makes a jQuery plugin out of a constructor :factory:
It's based off of the jQuery UI widget factory. You should probably use that, since it's very good. I use this, since it's a bit simpler. Used for Masonry, Isotope, and Packery.
Plugin constructor
A plugin constructor uses Prototypal pattern. It needs to have a ._init()
method used for its main logic.
function NiceGreeter( element, options ) {
this.element = $( element );
this.options = $.extend( true, {}, this.options, options );
this._init();
}
NiceGreeter.prototype.options = {
greeting: 'hello',
recipient: 'world'
};
NiceGreeter.prototype._init = function() {
var message = this.getMessage();
this.element.text( message );
};
NiceGreeter.prototype.getMessage = function() {
return this.options.greeting + ' ' + this.options.recipient;
};
Usage
Bridget can make this constructor work as a jQuery plugin. The namespace
is the plugin method - $elem.namespace()
.
$.bridget( 'niceGreeter', NiceGreeter );
var $elem = $('#elem');
$elem.niceGreeter();
$elem.niceGreeter({
greeting: 'bonjour',
recipient: 'mon ami'
});
var myGreeter = $elem.data('niceGreeter');
Getter methods can still be used. For jQuery objects with multiple elements, getter methods will return the value of the first element.
Package managers
Install with Bower :bird:: bower install jquery-bridget
Install with npm npm install jquery-bridget
Install with component: component install desandro/jquery-bridget
MIT license
Bridget is released under the MIT license.